使用 ngx.log() 时如何提供更多上下文信息?
How can I provide more context information when using ngx.log()?
我想为我们的 OpenResty 应用程序提供结构化日志记录。我们的目标是实现 MDC(映射诊断上下文)或非常相似的东西。
目前,调用 ngx.log(ngx.NOTICE, "foo")
会得到如下结果:
2019/01/31 17:39:50 [notice] 16#16: *1 [lua] example_logging.lua:20: myfunc(): foo, client: 172.17.0.1, server: _, request: "GET /example HTTP/1.1", host: "localhost:8080"
也就是说,日志记录会自动添加 key/value 对:
- 服务器
- 客户
- 请求
- 主机
是否有一种机制允许在不修改对 ngx.log()
的调用的情况下向其中添加更多 key/value 对,而是将所需数据注入其他地方?
应用程序正在使用 OpenResty 1.11.2.1-2。
我想这是不可能的。 ngx.log
只是一个很小的包装器(1, 2) for ngx_log_error
具有硬编码消息格式,ngx_log_error
格式也不可配置。
Old but still relevant comment from OpenResty core developer:
Well, ngx.log() is for error logging. And it just invokes nginx core's
error logger, which does not support custom format, unlike the access
logging.
我想为我们的 OpenResty 应用程序提供结构化日志记录。我们的目标是实现 MDC(映射诊断上下文)或非常相似的东西。
目前,调用 ngx.log(ngx.NOTICE, "foo")
会得到如下结果:
2019/01/31 17:39:50 [notice] 16#16: *1 [lua] example_logging.lua:20: myfunc(): foo, client: 172.17.0.1, server: _, request: "GET /example HTTP/1.1", host: "localhost:8080"
也就是说,日志记录会自动添加 key/value 对:
- 服务器
- 客户
- 请求
- 主机
是否有一种机制允许在不修改对 ngx.log()
的调用的情况下向其中添加更多 key/value 对,而是将所需数据注入其他地方?
应用程序正在使用 OpenResty 1.11.2.1-2。
我想这是不可能的。 ngx.log
只是一个很小的包装器(1, 2) for ngx_log_error
具有硬编码消息格式,ngx_log_error
格式也不可配置。
Old but still relevant comment from OpenResty core developer:
Well, ngx.log() is for error logging. And it just invokes nginx core's error logger, which does not support custom format, unlike the access logging.