You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2023/01/30 01:42:34 UTC

[apisix] 01/09: fix(file-loger): use no buffering model when open file (#7884)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch release/2.15
in repository https://gitbox.apache.org/repos/asf/apisix.git

commit c6c32e771a8db7730272668855c0b249a04b3fc4
Author: levy liu <37...@qq.com>
AuthorDate: Wed Nov 23 11:23:52 2022 +0800

    fix(file-loger): use no buffering model when open file (#7884)
    
    Fixes https://github.com/apache/apisix/issues/7839
---
 apisix/plugins/file-logger.lua | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/apisix/plugins/file-logger.lua b/apisix/plugins/file-logger.lua
index e624b0bbc..9fbe359a7 100644
--- a/apisix/plugins/file-logger.lua
+++ b/apisix/plugins/file-logger.lua
@@ -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
@@ -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