nginx自定义日志

自定义日志

全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和记录日志的level,因此有着本质的区别,而且Nginx的错误日志一般只有一个,但是访问日志可以在不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容
由 ngx_http_log_module 模块实现
定义日志格式只能在http配置段,调用日志格式只能在http、server、location、if、limit_except

官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html

更新:
记录请求体和响应体日志:参考链接

例1: 自定义默认日志格式
http {
	log_format nginx_fmt1 '$remote_addr - $remote_user [$time_local] "$request"'
		'$status $body_bytes_sent "$http_referer"'
		'"$http_user_agent" "$http_x_forwarded_for"'
		'$server_name:$server_port';

	server {
		access_log	logs/access.log nginx_fmt1;	
	}
}
例2: 自定义json格式日志

Nginx的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析

http {
	#记录请求体时,日志文件一天大概2,30m,比默认记录的要大很多,不关注此变量的可以去掉
	log_format acc_json escape=none
		'{"@timestamp": "$time_iso8601",'
		'"host": "$server_addr",'
		'"clientip": "$remote_addr",'
		'"size": "$body_bytes_sent",'
		'"responsetime": "$request_time",'
		'"upstreamtime": "$upstream_response_time",'
		'"upstreamhost": "$upstream_addr",'
		'"upstream_code": "$upstream_status",'
		'"http_host": "$host",'
		'"request_method": "$request_method",'
		'"uri": "$uri",'
		'"xff": "$http_x_forwarded_for",'
		'"referer": "$http_referer",'
		'"tcp_xff": "$proxy_protocol_addr",'
		'"request_body": "$request_body",'
		'"http_user_agent": "$http_user_agent",'
		'"status": "$status"}';

	#ip信息仅在开启http_geoip_module和三方模块ngx_http_geoip2_module时有效
	log_format json escape=json
		'{"@timestamp": "$time_iso8601",'
		'"host": "$server_addr",'
		'"clientip": "$remote_addr",'
		'"size": "$body_bytes_sent",'
		'"response_time": "$request_time",'
		'"upstream_time": "$upstream_response_time",'
		'"upstream_host": "$upstream_addr",'
		'"upstream_code": "$upstream_status",'
		'"http_host": "$host",'
		'"request_method": "$request_method",'
		'"uri": "$uri",'
		'"xff": "$http_x_forwarded_for",'
		'"referer": "$http_referer",'
		'"tcp_xff": "$proxy_protocol_addr",'
		'"request_body": "$request_body",'
		'"user_agent": "$http_user_agent",'
		'"status": "$status",'
		'"client_country_name":"$geoip2_country_name_cn | $geoip2_country_name_en",'
		'"client_city_name":"$geoip2_city_name_cn | $geoip2_city_name_en",'
		'"longitude_latitude":"$geoip2_longitude,$geoip2_latitude"}';



	server {
		access_log /opt/nginx/logs/json_acc.log acc_json;
	}
}
例3: py程序统计json格式的日志

py3的代码

#!/usr/bin/env python3
#coding:utf-8

status_200= []
status_404= []

with open("/opt/nginx/logs/json_acc.log") as f:
	for line in f.readlines():
		line = eval(line)
		if line.get("status") == "200":
			status_200.append(line.get)
		elif line.get("status") == "404":
			status_404.append(line.get)
		else:
			print("状态码 ERROR")
		print((line.get("clientip")))
f.close()
print("状态码200的有--:",len(status_200))
print("状态码404的有--:",len(status_404))

py2的代码

#!/usr/bin/env python
#coding:utf-8

status_200= []
status_404= []

with open("/opt/nginx/logs/json_acc.log") as f:
	for line in f.readlines():
		line = eval(line)
		if line.get("status") == "200":
			status_200.append(line.get)
		elif line.get("status") == "404":
			status_404.append(line.get)
		else:
			print("状态码 ERROR")
		print(line.get("clientip"))
f.close()
print "状态码200的有--:",len(status_200)
print "状态码404的有--:",len(status_404)
posted @ 2022-02-13 11:42  suyanhj  阅读(686)  评论(0)    收藏  举报