json - 如何从 nginx 生成 JSON 日志?

标签 json logging nginx

我正在尝试从 nginx 生成 JSON 日志。

我知道像 this one 这样的解决方案但我想记录的一些字段包括需要正确转义的用户生成的输入(如 HTTP header )。

我知道 2011 年 10 月和 2008 年 5 月的 nginx 更改日志条目说:

*) Change: now the 0x7F-0x1F characters are escaped as \xXX in an
   access_log.
*) Change: now the 0x00-0x1F, '"' and '\' characters are escaped as \xXX
   in an access_log.

但这仍然没有帮助,因为 \xXXinvalid in a JSON string .

我也看过 the HttpSetMiscModule具有 set_quote_json_str 指令的模块,但这似乎只是在没有帮助的字符串周围添加 \x22

对于其他从 nginx 以 JSON 格式登录的解决方案有什么想法吗?

最佳答案

最后看起来我们有一个很好的方法来使用没有任何模块的 vanilla nginx 来做到这一点。只需定义:

log_format json_combined escape=json
  '{'
    '"time_local":"$time_local",'
    '"remote_addr":"$remote_addr",'
    '"remote_user":"$remote_user",'
    '"request":"$request",'
    '"status": "$status",'
    '"body_bytes_sent":"$body_bytes_sent",'
    '"request_time":"$request_time",'
    '"http_referrer":"$http_referer",'
    '"http_user_agent":"$http_user_agent"'
  '}';

注意 escape=json 是在 nginx 1.11.8 中添加的。 http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

关于json - 如何从 nginx 生成 JSON 日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049667/

相关文章:

jquery - IE8 上 json 解析失败

ios - 如何在 Xcode 控制台中禁用 os_log_info 和 os_log_debug 消息?

gwt - ServletContext.log() 不记录

django - linux用户权限的最佳实践以如下方式运行Web应用程序?

Django REST AngularJS NGINX 配置

linux - 为什么要更改 NGINX 文件位置?

ajax - Extjs 自定义阅读器

ruby-on-rails - 将 JSON 导入 Rails

java - Logback sfl4j MDC对http请求是唯一的

json - "resolveJsonModule"的用途?