日志

  • 记录和分析日志可快速找到系统产生的问题
  • 统计日志可以作为系统优化和升级的依据

apache httpd日志

  • 日志文件默认路径
    /etc/httpd/logs/
  • 官方参考帮助:
  • http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats
[15:11:56 root@C8-88[ ~]#ll /etc/httpd/logs/
total 2308
-rw-r--r--  1 root root       0 May 26 03:45 access_log
-rw-r--r--. 1 root root    9593 May  5 00:34 access_log-20210505
-rw-r--r--  1 root root 1431877 May  7 20:37 access_log-20210512
-rw-r--r--  1 root root    6008 May 18 18:32 access_log-20210519
-rw-r--r--  1 root root  851104 May 21 13:08 access_log-20210526
-rw-r--r--  1 root root    1529 May 26 08:56 error_log
-rw-r--r--. 1 root root   14345 May  5 03:35 error_log-20210505
-rw-r--r--  1 root root    9720 May 12 09:19 error_log-20210512
-rw-r--r--  1 root root    8736 May 19 09:45 error_log-20210519
-rw-r--r--  1 root root   10704 May 26 03:45 error_log-20210526

httpd有两种日志类型

  • 访问日志
  • 错误日志

错误日志

#LogLevel 可选值: debug, info, notice, warn,error, crit, alert,
LogLevel warn 
emerg
ErrorLog logs/error_log

访问日志

  • 定义日志格式
    LogFormat format nickname
  • 使用日志格式
    CustomLog file nickname
LogFormat "%h %l %u [%{%F %T}t] \"%r\" %>s %b \"%{Referer}i\" \"%{User-
Agent}i\"" testlog

日志参数说明

  • %h #客户端IP地址
  • %l #远程用户,启用mod_ident才有效,通常为减号"-”
  • %u #验证(basic,digest)远程用户,非登录访问时,为一个减号"-”
  • %t #服务器收到请求时的时间
  • %r #First line of request,即表示请求报文的首行;记录了此次请求的"方法”,"URL”以及协议版本
  • %>s #响应状态码
  • %b #响应报文的大小,单位是字节;不包括响应报文http首部
  • %{Referer}i #请求报文中首部"referer”的值;即从哪个页面中的超链接跳转至当前页面的
  • %{User-Agent}i #请求报文中首部"User-Agent”的值;即发出请求的应用程序
  • %{VARNAME}i #The contents of VARNAME: header line(s) in the request sent to the server

自定义日志格式

  • 日志格式可以通过修改配置文件进行定制
  • 通过定义日志格式可以挑出想要的信息
[root@centos8 ~]#vim /etc/httpd/conf/httpd.conf
logFormat "%h \"%{%F %T}t\" %>s \"%{User-Agent}i\"" testlog
CustomLog "logs/access_log" testlog
[root@centos8 ~]#tail -f /var/log/httpd/access_log
10.0.0.88 "2021-05-26 15:26:51" 200 "curl/7.29.0"
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐