apache日志:error_log,access_log

Posted toDoYourBest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache日志:error_log,access_log相关的知识,希望对你有一定的参考价值。

参考资料

apache开启日志记录,access.log

Apache配置文件里的LogLevel指令说明

apache官方文档 apache logs

关于error log

今天在一台测试服务器上测试项目时,有一个请求返回了500状态码,web服务器使用的是apache,于是就查找apache错误日志,却发现日志是空的.好奇怪.

后来查资料,发现,错误记录也是有级别的

LevelDescriptionExample
emerg Emergencies - system is unusable.紧急状况;服务器无法使用  "Child cannot open lock file. Exiting"
alert Action must be taken immediately.必须立刻采取动作 "getpwuid: couldn‘t determine user name from uid"
crit Critical Conditions.危急状况 "socket: Failed to get a socket, exiting child" 
error Error conditions.出现错误  "Premature end of script headers"
warn Warning conditions.警告 "child process 1234 did not exit, sending another SIGHUP" 
notice Normal but significant condition.正常,但有情况要注意  "httpd: caught SIGBUS, attempting to dump core in ..."
info Informational.普通信息  "Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)..."
debug Debug-level messages.调试级别信息,包括模块运行状态 "Opening config file ..."
   

这个错误级别记录在apache的配置文件httpd.conf或者虚拟主机配置文件中

ErrorLog "logs/error_log"

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel alert

其中,LogLevel就指定了最低要记录的错误级别,我公司这台测试服务器原来的级别是alert,太高了,导致很多错误不会记录,因此我设置为info,重启apache,然后重现错误,终于在error_log中发现了错误原因:

[Tue Jul 25 17:33:06.476703 2017] [:error] [pid 2316:tid 3328] [client 113.47.63.8:49522] php Fatal error:  
Call to undefined function Awap\\Controller\\getLogger() in 项目路径\\Application\\Awap\\Controller\\AdvertiserController.class.php on line 533,
referer: http://www.xxxx.com/Awap/Advertiser/register

 

原来是php报了错,后来追项目的源码,发现是php返回了500的状态码.

那为什么apache 的error_log会记录PHP的错误呢,apache文档中是这样说的:

The error log will also contain debugging output from CGI scripts. 
Any information written to stderr by a CGI script will be copied directly to the error log.

意思是:php作为一个CGI脚本,它的标准错误(stderr)被复制了一份放在了apache error log中.

apache推荐最低级别为crit,具体设置为哪个,还是根据自己的需求来设定吧!

 

关于access_log

以下是我的笔记,大概记录了一下,详情请查看文章头部的链接

Related Modules:mod_log_config mod_setenvif

Related Directives:CustomLog LogFormat SetEnvIf

access log的作用:access log记录了所有服务器处理过的请求.

access log的存放路径和内容由 CustomLog指令控制,

使用LogFormat指令可以简单地指定在内容中需要显示的项目,

举例,下面代码中的黄色区域,CustomLog指定了access log的存放路径

<VirtualHost *:80>
    LogLevel info
    DocumentRoot "E:\qprwork\project"
    ErrorLog "logs/myapp.com-error.log"   
    CustomLog "logs/myapp.com-access.log" combined
    ServerName myapp.com
    <Directory   "E:\qprwork\project">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
</VirtualHost>

那个combined是啥?他是告诉log的记录格式的.这个就和LogFormat有关了

LogFormat指令分为两部分,第一部分是格式,第二部分为nickname,

 

以下摘自httpd.conf,蓝色就是LogFormat,其中左侧百分号那一串是格式,右侧combined是nickname

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    CustomLog "e:/wamp/logs/access.log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access.log" combined
</IfModule>

在定制CustomLog时,可以使用nickname指定需要的格式,如上面代码片段中的 CustomLog "logs/myapp.com-access.log"   combined 

以下是apache的access_log片段

127.0.0.1 - - [25/Jul/2017:22:42:06 +0800] "GET /advertiser/index/monolog HTTP/1.1" 200 195 "-" 
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/59.0.3071.115 Safari/537.36" 127.0.0.1 - - [25/Jul/2017:22:42:06 +0800] "GET /favicon.ico HTTP/1.1" 200 1150 "http://myapp.com/advertiser/index/monolog"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

 






以上是关于apache日志:error_log,access_log的主要内容,如果未能解决你的问题,请参考以下文章

apache日志及日志分割

Apache日志配置参数详细说明

Apache按天截断日志工具,无法生成访问日志

nginx的access_log与error_log

nginx日志-错误日志(error_log)

怎么关闭nginx的日志