Skip to content

Commit

Permalink
fix(file-loger): use no buffering model when open file (apache#7884)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 authored and spacewander committed Jan 28, 2023
1 parent e3bdf27 commit 3c80bf3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions apisix/plugins/file-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ if is_apisix_or then
return nil, err
end

-- it will case output problem with buffer when log is larger than buffer
file:setvbuf("no")

handler.file = file
handler.open_time = ngx.now() * 1000
return handler
Expand Down Expand Up @@ -116,11 +119,14 @@ local function write_file_data(conf, log_message)
if not file then
core.log.error("failed to open file: ", conf.path, ", error info: ", err)
else
local ok, err = file:write(msg, '\n')
-- file:write(msg, "\n") will call fwrite several times
-- which will cause problem with the log output
-- it should be atomic
msg = msg .. "\n"
-- write to file directly, no need flush
local ok, err = file:write(msg)
if not ok then
core.log.error("failed to write file: ", conf.path, ", error info: ", err)
else
file:flush()
end

-- file will be closed by gc, if open_file_cache exists
Expand Down

0 comments on commit 3c80bf3

Please sign in to comment.