<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 正確的記錄日志 看過本章第一節的同學應該還記得,log_by_lua是一個會話階段最后發生的,文件操作是阻塞的(FreeBSD直接無視),nginx為了實時高效的給請求方應答后,日志記錄是在應答后異步記錄完成的。由此可見如果我們有日志輸出的情況,最好統一到log_by_lua階段。如果我們自定義放在content_by_lua階段,那么將線性的增加請求處理時間。 在公司某個定制化項目中,nginx上的日志內容都要輸送到syslog日志服務器。我們使用了[lua-resty-logger-socket](https://github.com/cloudflare/lua-resty-logger-socket)這個庫。 > 調用示例代碼如下(有問題的): ~~~ -- lua_package_path "/path/to/lua-resty-logger-socket/lib/?.lua;;"; -- -- server { -- location / { -- content_by_lua lua/log.lua; -- } -- } -- lua/log.lua local logger = require "resty.logger.socket" if not logger.initted() then local ok, err = logger.init{ host = 'xxx', port = 1234, flush_limit = 1, --日志長度大于flush_limit的時候會將msg信息推送一次 drop_limit = 99999, } if not ok then ngx.log(ngx.ERR, "failed to initialize the logger: ",err) return end end local msg = string.format(.....) local bytes, err = logger.log(msg) if err then ngx.log(ngx.ERR, "failed to log message: ", err) return end ~~~ 在實測過程中我們發現了些問題: - 緩存無效:如果flush_limit的值稍大一些(例如 2000),會導致某些體積比較小的日志出現莫名其妙的丟失,所以我們只能把flush_limit調整的很小 - 自己拼寫msg所有內容,比較辛苦 那么我們來看[lua-resty-logger-socket](https://github.com/cloudflare/lua-resty-logger-socket)這個庫的log函數是如何實現的呢,代碼如下: ~~~ function _M.log(msg) ... if (debug) then ngx.update_time() ngx_log(DEBUG, ngx.now(), ":log message length: " .. #msg) end local msg_len = #msg if (is_exiting()) then exiting = true _write_buffer(msg) _flush_buffer() if (debug) then ngx_log(DEBUG, "Nginx worker is exiting") end bytes = 0 elseif (msg_len + buffer_size < flush_limit) then -- 歷史日志大小+本地日志大小小于推送上限 _write_buffer(msg) bytes = msg_len elseif (msg_len + buffer_size <= drop_limit) then _write_buffer(msg) _flush_buffer() bytes = msg_len else _flush_buffer() if (debug) then ngx_log(DEBUG, "logger buffer is full, this log message will be " .. "dropped") end bytes = 0 --- this log message doesn't fit in buffer, drop it ... ~~~ 由于在content_by_lua階段變量的生命周期會隨著會話的終結而終結,所以當日志量小于flush_limit的情況下這些日志就不能被累積,也不會觸發_flush_buffer函數,所以小日志會丟失。 這些坑回頭看來這么明顯,所有的問題都是因為我們把lua/log.lua用錯階段了,應該放到log_by_lua階段,所有的問題都不復存在。 > 修正后: ~~~ lua_package_path "/path/to/lua-resty-logger-socket/lib/?.lua;;"; server { location / { content_by_lua lua/content.lua; log_by_lua lua/log.lua; } } ~~~ 這里有個新問題,如果我的log里面需要輸出一些content的臨時變量,兩階段之間如何傳遞參數呢? > 方法肯定有,推薦下面這個: ~~~ location /test { rewrite_by_lua ' ngx.say("foo = ", ngx.ctx.foo) ngx.ctx.foo = 76 '; access_by_lua ' ngx.ctx.foo = ngx.ctx.foo + 3 '; content_by_lua ' ngx.say(ngx.ctx.foo) '; } ~~~ 更多有關ngx.ctx信息,請看[這里](http://wiki.nginx.org/HttpLuaModuleZh#ngx.ctx)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看