ngx.log函数如何使用

2,706 views
Skip to first unread message

朱茂海

unread,
Oct 28, 2012, 9:19:38 PM10/28/12
to open...@googlegroups.com
文档是这样写的:
syntax: ngx.log(log_level, ...)

我就在lua代码中
if (error) then
     ngx.log(ngx.NOTICE,"lua had an errors")
end
nginx目前的日志等级是notice,这样写似乎没效果。

smallfish

unread,
Oct 28, 2012, 9:38:59 PM10/28/12
to open...@googlegroups.com
应该是没到 if error 分支里面吧。你在 if 之前加一个 NOTICE 的 log 试试。
请再确认一下你的 nginx 中 error.log 的日志等级。:)
--


2012/10/29 朱茂海 <z3292...@gmail.com>

朱茂海

unread,
Oct 28, 2012, 9:42:54 PM10/28/12
to open...@googlegroups.com
nginx的错误等级为notice,现在把ngx.NOTICE改为ngx.ERR就正常了,但是ngx.NOTICE不是包含ERR吗?

agentzh

unread,
Oct 28, 2012, 10:18:13 PM10/28/12
to open...@googlegroups.com
Hello!

2012/10/28 朱茂海:
> nginx的错误等级为notice,现在把ngx.NOTICE改为ngx.ERR就正常了,但是ngx.NOTICE不是包含ERR吗?
>

可以尝试下面这个最简单的例子:

error_log logs/error.log notice;

http {
server {
location /t {
content_by_lua '
ngx.log(ngx.NOTICE, "hello")
';
}
}

在我这里访问 /t 接口可以在 logs/error.log 文件中得到下面这条消息:

2012/10/28 19:13:17 [notice] 6540#0: *1 [lua] [string
"content_by_lua"]:2: hello, client: 127.0.0.1, server: localhost,
request: "GET /t HTTP/1.1", host: "localhost"

一个建议是尽量使用最简单的例子来验证你的假设,这样经常可以发现配置上的低级错误,或者可以确认 ngx_lua 的潜在 bug(如果有的话)。

Best regards,
-agentzh

Message has been deleted

Yichun Zhang (agentzh)

unread,
Aug 30, 2016, 7:34:18 PM8/30/16
to openresty
Hello!

2016-08-28 8:04 GMT-07:00 何兆阳:
> I met the same question also,
> when I use ngx.ERR, it can be added in the error.log
> but when I use ngx.NOTICE, it can't work
>
> also when I use the simplest example.
>

http://nginx.org/en/docs/ngx_core_module.html#error_log

如果你自己在 nginx.conf 配置里面过滤掉了 error 级别以下的日志,你自然看不到 notice 和 info 这些级别的日志了。

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