Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(file-loger): use no buffering model when open file #7884

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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