【发布时间】:2014-11-29 03:08:16
【问题描述】:
我想在我的 lua 代码中插入日志点(io.write),该代码本身位于 nginx 配置中(使用 HttpLuaModule 用于 nginx)。 怎么做? 访问和错误日志没有显示它们。
【问题讨论】:
我想在我的 lua 代码中插入日志点(io.write),该代码本身位于 nginx 配置中(使用 HttpLuaModule 用于 nginx)。 怎么做? 访问和错误日志没有显示它们。
【问题讨论】:
在 nginx 下运行时,应该使用 ngx.log。例如:
ngx.log(ngx.STDERR, 'your message here')
有关工作示例,请参阅http://linuxfiddle.net/f/77630edc-b851-487c-b2c8-aa6c9b858ebb
【讨论】:
在 nginx 中进行 Lua 调试的另一个选择(除了“打印”)是使用支持远程调试的 Lua IDE。我在instructions 上发布了如何使用 ZeroBrane Studio IDE 完成此操作。设置完成后,您将获得您期望的大部分调试功能,例如单步执行、断点、变量检查、堆栈跟踪以及远程运行 Lua 命令的控制台。
【讨论】:
openresty 的另一个故障排除工具是lua-resty-console,它允许您检查 lua 变量、导入的模块、调用函数等。 你所要做的就是使用 luarocks 安装它并在你的 nignx.conf 中插入几行。启动客户端,然后就可以了。
[9] ngx(content)> ngx.config.prefix()
=> /workspace/lua-resty-console/
[10] ngx(content)> ngx.config.ngx_lua_version
=> 10011
[11] ngx(content)> ngx.config.nginx_configure()
=> --prefix=/usr/local/Cellar/openresty/1.13.6.1/nginx --with-cc-opt='-O2 -I/usr/local/include -I/usr/local/opt/pcre/include -I/usr/local/opt/openresty-openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 ...
[12] ngx(content)> ngx.sha →→
ngx.sha1_bin() ngx.shared.
[12] ngx(content)> ngx.shared. →→
ngx.shared.mycache. ngx.shared.metrics.
[12] ngx(content)> c = ngx.shared.mycache
=> nil
[13] ngx(content)> c
=> { <userdata 1>,
<metatable> = <1>{
__index = <table 1>,
add = <function 1>,
delete = <function 2>,
flush_all = <function 3>,
flush_expired = <function 4>,
get = <function 5>,
get_keys = <function 6>,
get_stale = <function 7>,
incr = <function 8>,
llen = <function 9>,
lpop = <function 10>,
lpush = <function 11>,
replace = <function 12>,
rpop = <function 13>,
rpush = <function 14>,
safe_add = <function 15>,
safe_set = <function 16>,
set = <function 17>
}
}
[14] ngx(content)> c:set('a', 1)
=> true
[15] ngx(content)> c:get('a')
=> 1
[16] ngx(content)> c:get_keys()
=> { "a" }
【讨论】: