Indeed, this is not something likely to happen in a production
environment with substantial traffic; we use the socket logger to log
only specific events that are sometimes uncommon, and I wanted to make
sure to avoid a potential bug with this. Thanks for the patch!
I think I am having a little trouble understanding the scope of the
logger. Since it is declared local in the init_worker_by_lua scope,
doesn't that mean that it will not be available in separate phases of
subsequent requests, or in a separate module? Or am I misunderstanding
the scope of locally-declared lua variables?
On 08/11/2014 04:26 PM, Yichun Zhang (agentzh) wrote:
> Hello!
>
> On Sun, Aug 10, 2014 at 10:52 PM, Robert Paprocki wrote:
>> We have stated implementing lua-resty-logger-socket to log to a customer
>> log handler; however, we are finding that because it seems that the
>> logger does not regularly flush the buffer, data can sit stale in the
>> buffer in the openresty server for a long time (minutes to hours) if no
>> additional bytes are sent to the logger. Any recommendations on
>> regularly flushing the buffer (such as every 60 seconds)? I would think
>> using ngx.timer would be appropriate but I haven't been able to nail
>> down an appropriate practice for such. Thanks!
>>
>
> Seems like your server may have not enough traffic sometimes which
> seldom or never happen in our production environments :)
>
> Yes, you can use a timer to periodically flush buffered messages. Just
> apply the following patch to your lua-resty-logger-socket:
>
> http://openresty.org/download/0001-feature-added-flush-method.patch
>
> And add the following snippet to the http {} block in your nginx.conf file:
>
> init_worker_by_lua '
> local logger = require "resty.logger.socket"
> local do_flush -- forward declaration
> local function install_timer()
> local ok, err = ngx.timer.at(60, do_flush) -- 60 sec
> if not ok then ngx.log(ngx.ERR, "failed to create timer for
> flushing logs: ", err) end
> end
> function do_flush(premature)
> if premature then return end
> if logger.initted() then logger.flush() end
> install_timer()
> end
> install_timer()
> ';
>
> Regards,
> -agentzh
>