Openresty中使用ngx.log()来调试

978次阅读
没有评论

共计 2607 个字符,预计需要花费 7 分钟才能阅读完成。

Openresty中使用ngx.log()来调试

刚学习openresty时,匆匆看了看lua基本语法知识,就开始上手写些简单的示例demo。期间总会遇到报错,需要调试,此时就可以在openresty中公用ngx.log()方法来打印输出到日志里,目前也就只知道这个方式方式来调试。。。

nginx 错误日志级别有以下几个

-- 越上面,日志等级越高,信息越少
-- 指定了日志级别只会输出比当前级别高的日志,比如当前级别为err,则warn/notice/info/debug级别的信息不会记录在日志中
--- OpenResty 里面的 print 语句是 INFO 级别
ngx.STDERR     -- 标准输出
ngx.EMERG      -- 紧急报错
ngx.ALERT      -- 报警
ngx.CRIT       -- 严重,系统故障,触发运维告警系统
ngx.ERR        -- 错误,业务不可恢复性错误
ngx.WARN       -- 告警,业务中可忽略错误
ngx.NOTICE     -- 提醒,业务比较重要信息
ngx.INFO       -- 信息,业务琐碎日志信息,包含不同情况判断等
ngx.DEBUG      -- 调试

错误日志级别在nginx http块外配置

[root@nginx-cluster conf]# cat nginx.conf
worker_processes  4;
events {
    worker_connections  65535;
}

# 配置info级别
error_log logs/error.log info;

http {
    ....略....
}

info级别日志样例

2021/05/02 01:01:10 [warn] 1567#0: 65535 worker_connections exceed open file resource limit: 1024
2021/05/02 01:02:25 [alert] 1630#0: lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:12
2021/05/02 01:02:25 [warn] 1630#0: 65535 worker_connections exceed open file resource limit: 1024
2021/05/02 01:02:34 [alert] 1659#0: lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:12
2021/05/02 01:02:34 [warn] 1659#0: 65535 worker_connections exceed open file resource limit: 1024
2021/05/02 01:02:34 [alert] 1661#0: lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:12
2021/05/02 01:02:34 [notice] 1661#0: using the "epoll" event method
2021/05/02 01:02:34 [warn] 1661#0: 65535 worker_connections exceed open file resource limit: 1024
2021/05/02 01:02:34 [notice] 1661#0: openresty/1.17.8.2
2021/05/02 01:02:34 [notice] 1661#0: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
2021/05/02 01:02:34 [notice] 1661#0: OS: Linux 3.10.0-862.el7.x86_64
2021/05/02 01:02:34 [notice] 1661#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2021/05/02 01:02:34 [notice] 1663#0: start worker processes
2021/05/02 01:02:34 [notice] 1663#0: start worker process 1664
2021/05/02 01:02:34 [notice] 1663#0: start worker process 1665
2021/05/02 01:02:34 [notice] 1663#0: start worker process 1666
2021/05/02 01:02:34 [notice] 1663#0: start worker process 1667
2021/05/02 01:02:45 [info] 1666#0: *1 client 192.168.44.145 closed keepalive connection
2021/05/02 01:03:34 [info] 1667#0: *2 client 192.168.44.145 closed keepalive connection

一个简单的server样例,使用ngx.log()来调试打印一些变量

    server {
        listen    80;
        location / {
            content_by_lua_block {
                local num = 55
                local str = "string"
                local obj
                ngx.log(ngx.ERR, "num:", num)
                ngx.log(ngx.INFO, " string:", str)
                print([[i am print]])
                ngx.log(ngx.ERR, " object:", obj)
            }
        }
    }

日志输出

2021/05/02 01:05:34 [error] 61610#0: *10 [lua] content_by_lua(nginx.conf:26):5:
 num:55, client: 192.168.44.145, server: , request: "GET /hello HTTP/1.1",
 host: "192.168.44.145"
2021/05/02 01:05:38 [error] 61610#0: *10 [lua] content_by_lua(nginx.conf:26):7:
 object:nil, client: 192.168.44.145, server: , request: "GET /hello HTTP/1.1",
 host: "192.168.44.145"

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2021-05-02发表,共计2607字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)