Error logging to syslog inside init_worker_by_lua

410 views
Skip to first unread message

Chris Tanner

unread,
Feb 3, 2015, 8:55:52 AM2/3/15
to openre...@googlegroups.com
Hello!

I'm trying to redirect all error and access logging to syslog-ng using nginx's built in directives. When accessing a basic content by Lua page that runs 'ngx.log(ngx.ERR, "testerror")', i correctly get two syslog entries: one for access and one with the error. 
When I attempt to log inside a timer "ngx.log(ngx.ERR, 'recurring timer on all workers')" however, the error logs to the default file location of logs/error.log.
 
In my nginx.conf (at http level):
log_format accessFormat '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent host:$host'
                       '"$http_referer" "$http_user_agent" ';

  access_log syslog:server=127.0.0.1,tag=access accessFormat;
  error_log syslog:server=127.0.0.1,tag=error;

If anyone could suggest what I'm doing wrong then I'd really appreciate it, alternately if you think it's more a core nginx issue please let me know and I'll post it over there.

Cheers! 
Chris


Yichun Zhang (agentzh)

unread,
Feb 3, 2015, 7:51:47 PM2/3/15
to openresty-en
Hello!

On Tue, Feb 3, 2015 at 5:55 AM, Chris Tanner <m...@itschr.is> wrote:
> When I attempt to log inside a timer "ngx.log(ngx.ERR, 'recurring timer on
> all workers')" however, the error logs to the default file location of
> logs/error.log.
>

Try putting your error_log configuration directive on the top level
(outside http {}). It should work.

I'm not sure right now why http {} does not work. It requires more
investigations. You're welcome to dig in.

Regards,
-agentzh

Chris Tanner

unread,
Feb 4, 2015, 5:37:18 AM2/4/15
to openre...@googlegroups.com
Thanks,  

I've tried that but without much luck. I'll have a dig through the source and see if I can work out whats going on.

Cheers,
Chris

Yichun Zhang (agentzh)

unread,
Feb 4, 2015, 3:08:02 PM2/4/15
to openresty-en
Hello!

On Wed, Feb 4, 2015 at 2:37 AM, Chris Tanner wrote:
> I've tried that but without much luck.

I tried OpenResty 1.7.7.2 RC2 yesterday with the following minimal example:

error_log syslog:server=127.0.0.1:12345 error;

http {
init_worker_by_lua '
ngx.log(ngx.ERR, "error from init worker")
ngx.timer.at(0, function ()
ngx.log(ngx.ERR, "error from timer")
end)
';

server { ... }
}

Then I used nc to emulate a syslog server like this:

nc -u -l 12345

Then I started the server and got the following output from nc:

<187>Feb 4 12:05:45 w530 nginx: 2015/02/04 12:05:45 [error]
4868#0: [lua] init_worker_by_lua:2: error from init worker, context:
init_worker_by_lua*<187>Feb 4 12:05:45 w530 nginx: 2015/02/04
12:05:45 [error] 4868#0: [lua] init_worker_by_lua:4: error from timer,
context: ngx.timer

We can see that it works as expected.

BTW, please ensure that you're using the latest OpenResty (or nginx +
ngx_lua). I've heard before that some people have been using this
configuration in production without problems.

Regards,
-agentzh

Chris Tanner

unread,
Feb 4, 2015, 5:55:10 PM2/4/15
to openre...@googlegroups.com
Thanks!

As suggested I've updated from 1.7.7.1 to 1.7.7.2 and its now logging to syslog correctly from a timer. It's also still logging to logs/error.log but maybe that's by design? Either way not a huge deal. 

Thanks again for your help!

Cheers,
Chris

Yichun Zhang (agentzh)

unread,
Feb 4, 2015, 6:09:00 PM2/4/15
to openresty-en
Hello!

On Wed, Feb 4, 2015 at 2:55 PM, Chris Tanner wrote:
> As suggested I've updated from 1.7.7.1 to 1.7.7.2 and its now logging to
> syslog correctly from a timer.

Cool.

> It's also still logging to logs/error.log but
> maybe that's by design?

This is the expected behavior of the nginx core. Not my design though :)

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages