request_body中含有中文时,nginx日志会转换为十六进制。
logstash为了能高效的处理各类日志,希望日志是一种特定结构存储的方式。
nginx默认日志格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" $request_body';
问题日志
[25/Feb/2019:00:00:10 +0800]|192.168.10.19|POST /paas/callback HTTP/1.1|200|163|0.002|0.002|{\x22rc\x22:0,\x22msg\x22:\x22success\x22,\x22transferrate\x22:\x2245301\x22}]}}
request_body 中都是转换后十六进制,不易阅读.
直接提供可视化日志格式和解决十六进制的解决办法: escape=json
格式化数据:
json格式:
log_format log_json escape=json '{"timestamp": "$time_local",' '"remote_addr": "$remote_addr",' '"referer": "$http_referer",' '"request": "$request",' '"statu": "$status",' '"byte": "$body_bytes_sent",' '"agen": "$http_user_agent",' '"x_forwarde": "$http_x_forwarded_for",' '"up_addr": "$upstream_addr",' '"up_host": "$upstream_http_host",' '"up_resp_time": "$upstream_response_time",' '"request_time": "$request_time"}';
log_format :日志格式开头
main :日志名称
escape=json :nginx 1.11.8版本后才提供此参数
ingress 的 log-format-escape-json 可以设置为:
"false" sets log_format escape=default
"true" sets log_format escape=json
但是 log_format 提供了 none,default,json三个参数,不能涵盖全!
With escape=json:
{\"status\":\"200\",\"request_query\":{\"foo\":\"bar\"}}
With escape=default:
{\x22status\x22:\x22request_query\x22:{\x22foo\x22:\x22bar\x22}}
With escape=none:
{"status":"200","request_query":{"foo":"bar"}}
新版本的 ingress nginx 已经解决:
取消了 log-format-escape-json 参数; 使用 log-format-escaping
如果使用老版本的 ingress 只能换日志格式为 json 了
共 0 条评论