码迷,mamicode.com
首页 > 其他好文 > 详细

自定义访问日志

时间:2019-06-02 21:50:42      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:utf-8   lis   vim   name   referer   pwd   passwd   gen   request   

注意黑体部分
[root@CentOS7 conf]#pwd
/apps/nginx/conf
[root@CentOS7 conf]#vim server/www_yunlong_net.conf

1 server {
2 listen 80;
3 server_name www.yunlong.net;
4 access_log /apps/nginx/logs/www_yunlong_net_access.log;
5 error_log /apps/nginx/logs/www_yunlong_net_error.log;
6
7 location / {
8 root /data/nginx/html/pc;
9 index index.html;
10 }
11 location /image {
12 index index.html;
13 root /data/nginx/html/pc;
14 }
15 location /login {
16 index index.html;
17 root /data/nginx/html/pc;
18 auth_basic "login password";
19 auth_basic_user_file /apps/nginx/conf/.htpasswd;
20 }
21 }

www.yunlong.net日志分类存放于黑体路径下

通过访问www.yunlong.net
tail -f /apps/nginx/logs/www_yunlong_net_access.log 可查看相应日志

进入主配置文件,在http下添加自定义json格式日志
http {
include mime.types;
default_type application/octet-stream;

#log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
#                  ‘$status $body_bytes_sent "$http_referer" ‘
#                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

#access_log  logs/access.log  main;

log_format access_json ‘{"@timestamp":"$time_iso8601",‘
 ‘"host":"$server_addr",‘
 ‘"clientip":"$remote_addr",‘
 ‘"size":$body_bytes_sent,‘
 ‘"responsetime":$request_time,‘
 ‘"upstreamtime":"$upstream_response_time",‘
 ‘"upstreamhost":"$upstream_addr",‘
 ‘"http_host":"$host",‘
 ‘"uri":"$uri",‘
 ‘"domain":"$host",‘
 ‘"xff":"$http_x_forwarded_for",‘
 ‘"referer":"$http_referer",‘
 ‘"tcp_xff":"$proxy_protocol_addr",‘
 ‘"http_user_agent":"$http_user_agent",‘
 ‘"status":"$status"}‘;
access_log  /apps/nginx/logs/access_json.log  access_json;        

进入辅助配置文件添加access_json
server{
2 listen 80;
3 server_name www.yunlong.net;
4
5 access_log /apps/nginx/logs/www_yunlong_net_access.log access_json;
6
7 location /echo {
8 echo hello;
9 default_type text/html;
10 }
11
12 location /main {
13 index index.html;
14 default_type text/html;
15
16 set $name yunlong;
17 echo $name;
18 }
19
20 location /sub1 {
21 echo_sleep 1;
22 echo sub1;
23 }
24
25 location /sub2 {
26 echo_sleep 1;
27 echo sub2;
28 }
29
30 }

导入一个json格式的日志文件access_json.log
编写py脚本
vim log.py
#!/usr/bin/env python
#coding:utf-8
#Author:xxx
status_200= []
status_404= []
with open("access_json.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")
f.close()

print "状态码200的有--",len(status_200)
print "状态码404的有--",len(status_404)

执行脚本
python log.py

自定义访问日志

标签:utf-8   lis   vim   name   referer   pwd   passwd   gen   request   

原文地址:https://blog.51cto.com/14231434/2403964

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!