openresty ngx_lua日志操作


openresty ngx_lua日志操作

        

              

                                   

日志操作

            

ngx.log:向日志文件输出日志

语法格式:ngx.log(log_level, ...)

环境:init_by_lua*, init_worker_by_lua*, log_by_lua*, ngx.timer.*, 
     set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, 
     header_filter_by_lua*, body_filter_by_lua*, balancer_by_lua*, 
     ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, 
     ssl_session_store_by_lua*, ssl_client_hello_by_lua*, 
     exit_worker_by_lua*

              

日志级别

ngx.STDERR:错误信息输出到控制台
ngx.EMERG:紧急错误
ngx.ALERT:警告
ngx.CRIT:严重问题
ngx.ERR:错误

ngx.WARN:告警,一般可以忽略
ngx.NOTICE:通知
ngx.INFO:打印信息,生产环境一般不使用
ngx.DEBUG:调试日志,一般在开发测试的时候使用

            

error_log 指令

指令格式:error_log path/file level
* level可选值:debug、info、notice、warn、error(默认)、crit等
* 设置区域:main、http、server、location

# 关闭错误日志
error_log /dev/null

          

                

                                   

使用示例

            

default.conf

server {
    listen       80;
    server_name  localhost;

    # 设置错误日志输出级别为debug
    error_log logs/error.log debug;

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    location /test {
        content_by_lua_block {
            ngx.log(ngx.STDERR, "stderr log");
            ngx.log(ngx.EMERG, "emerg log");
            ngx.log(ngx.ALERT, "alert log");
            ngx.log(ngx.CRIT, "crit log");
            ngx.log(ngx.ERR, "error log");

            ngx.log(ngx.WARN, "warn log");
            ngx.log(ngx.NOTICE, "notice log");
            ngx.log(ngx.INFO, "info log");
            ngx.log(ngx.DEBUG, "debug log");
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

        

创建容器

docker run -it -d -p 4006:80 \
-v /Users/huli/lua/openresty/log/default.conf:/etc/nginx/conf.d/default.conf \
--name open openresty/openresty

       

使用测试

# 调用/test
huli@hudeMacBook-Pro log % curl localhost:4006/test

# 查看日志
huli@hudeMacBook-Pro log % docker logs open
2022/07/11 05:16:50 [] 7#7: *1 [lua] content_by_lua(default.conf:24):2: stderr log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [emerg] 7#7: *1 [lua] content_by_lua(default.conf:24):3: emerg log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [alert] 7#7: *1 [lua] content_by_lua(default.conf:24):4: alert log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [crit] 7#7: *1 [lua] content_by_lua(default.conf:24):5: crit log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [error] 7#7: *1 [lua] content_by_lua(default.conf:24):6: error log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [warn] 7#7: *1 [lua] content_by_lua(default.conf:24):8: warn log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [notice] 7#7: *1 [lua] content_by_lua(default.conf:24):9: notice log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [info] 7#7: *1 [lua] content_by_lua(default.conf:24):10: info log, client: 172.17.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4006"
2022/07/11 05:16:50 [debug] 7#7: *1 [lua] content_by_lua(default.conf:24):11: debug log
172.17.0.1 - - [11/Jul/2022:05:16:50 +0000] "GET /test HTTP/1.1" 200 5 "-" "curl/7.77.0"
2022/07/11 05:16:50 [info] 7#7: *1 client 172.17.0.1 closed keepalive connection

          

             

你可能感兴趣的:(openresty,openresty)