nginx+lua openresty开发系列-(log日志详解),nginxopenresty


最近搭建流媒体服务器,其中涉及到一些http服务api的操作, 之前一直使用的是python django框架来处理这些信息, 这次编译的是nginx添加了lua模块, 就想着使用lua来完成这些功能, 减少服务的搭建。好久没有lua开发了,现在突然发现好多东西都记不住了, 所以就借着这次开发, 记录下经历的开发经历, 及必备所需的东西, 以备后续查找翻阅以及分享给大家。

日志, 是一个开发必备的东西, 特别是服务器,处理并发的服务器所一定调试要用到的。之前刚开始的时候还曾使用过使用文件io创建, 文件进行记录相关日志, 结果过程中出现的好多问题, 自己给自己弄得焦头烂额,后来主攻了这部分内容,最近在写lua接口的时候不知道怎么用了, 查询日志类型的时候,没有找到正确的地方, 日志类型也只查到了error级别, 造成了困扰,所以才有了写这系列的想法。

废话少说, 在学习一个领域的时候一定要有相应好的学习的网址来学习, 不管谁写的博客,资料,还是最原始的比较好
https://github.com/openresty/lua-nginx-module
nginx 所有模块说明
http://nginx.org/en/docs/
这都是最原始的资料, 里面也是最官方的说明, 内容非常好,必备网址
openresty是nginx lua的打包程序并对其做了优化, 所以openresty的说明也是官方,必备, 有一点特别好, 中国人自己写的, 所以有中文版,对于像我这样英文不怎么好的人来说就是一个福音
https://legacy.gitbook.com/book/moonbingbing/openresty-best-practices/details
http://openresty.org/cn/
那搭建openresy nginx+lua开发环境呢,我本身是做流媒体开发的,所以我都是整体编译的。环境搭建过程请看我之前博客
https://blog.csdn.net/u012618915/article/details/81180421
以下是正题:

日志级别:
ngx.STDERR 标准输出
ngx.EMERG 紧急报错
ngx.ALERT 报警
ngx.CRIT 严重,系统故障, 触发运维告警系统
ngx.ERR 错误,业务不可恢复性错误
ngx.WARN 提醒, 业务中可忽略错误
ngx.NOTICE 提醒, 业务中比较重要信息
ngx.INFO 信息, 业务琐碎日志信息, 包含不同情况判断等
ngx.DEBUG 调试

这些都是常量, 越往上等级越高。

函数原型
ngx.log(level, …)
基本都是在content阶段使用
示例
ngx.log(ngx.ERR, “num:”, num)
ngx.log(ngx.INFO, ” string:”.. str)

注意 print语句是INFO级别

lua中日志完成了, 那如何设置日志格式,日志格式呢, 那就需要使用nginx本身的log_format 进行设置了
log_format 属于 ngx_http_log_module

示例:
log_format main ‘remoteaddrremote_user [timelocal]"request” ’
statusbody_bytes_sent “httprefererhttp_user_agent" "$http_x_forwarded_for”’;
这是我使用的日志格式
语法:
log_format name [escape=default|json|none] string …;

默认的log_format 为
log_format combined ‘remoteaddrremote_user [timelocal]request" statusbody_bytes_sent ’
‘”httpreferer""http_user_agent”’;

现在开始设置日志输出level, 那如何设置日志级别呢, 那就需要使用nginx本身的error_log进行设置了,
error_log属于ngx_core_module
示例:
error_log logs/error.log error;
语法
error_log file [level];
默认的error_log为
error_log logs/error.log error;
上下文为:
main, http, mail, stream, server, location
level 等级
debug, info, notice, warn, error, crit, alert, emerg
大于等于设置等级的日志均会被记录
若要设置debug level则在编译的时候添加–with-debug 即 ./configure –with-debug

以上为日志基本常用的功能均已经介绍完,以下为高阶功能, 不常用
远程日志:
lua-resty-logger-socket
以非阻塞 IO 方式推送 access log 到远程服务器上。 对远程服务器的要求是支持 syslog-ng 的日志服务
示例
log_by_lua_block {
local logger = require “resty.logger.socket”
if not logger.initted() then
local ok, err = logger.init{
host = ‘xxx’,
port = 1234,
flush_limit = 1234,
drop_limit = 5678,
}
if not ok then
ngx.log(ngx.ERR, “failed to initialize the logger: “,err)
return
end
end
local bytes, err = logger.log(msg)
if err then
ngx.log(ngx.ERR, “failed to log message: “, err)
return
end
优点:
基于 cosocket 非阻塞 IO 实现
日志累计到一定量, 集体提交, 增加网络传输利用率
短时间的网络抖动, 自动容错
日志累计到一定量, 如果没有传输完毕, 直接丢弃
日志传输过程完全不落地, 没有任何磁盘 IO 消耗

选择记录客户端debug日志 加编译选项(–with-debug)
示例
events {
debug_connection 192.168.1.1;
debug_connection 192.168.10.0/24;
}
日志为循环内存
error_log memory:32m debug;
这样做不影响高并发情况

gdb时可以这么用
set $log = ngx_cycle->log

while log>writer!=ngxlogmemorywritersetlog = $log->next
end

set buf=(ngxlogmemorybuft)log->wdata
dump binary memory debug_log.txt buf>startbuf->end

如果觉得有用, 请关注我的博客!!!!
做专注最接地气流媒体相关内容!!!!
我以后也会尽可能,尽自己最大水平持续更新!!!!

相关内容

    暂无相关文章