apache日志:error_log,access_log

参考资料

apache开启日志记录,access.log

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

apache官方文档 apache logs

关于error log

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

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

Level Description Example
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"
时间: 2024-08-29 05:22:12

apache日志:error_log,access_log的相关文章

Apache日志解读

想要知道什么人在什么时候浏览了网站的哪些内容吗?查看Apache的访问日志就可以知道.访问日志是Apache的标准日志,本文详细解释了访问日志的内容以及相关选项的配置. 一.访问日志的格式  Apache内建了记录服务器活动的功能,这就是它的日志功能.这个<Apache日志>系列文章介绍的就是Apache的访问日志.错误日志,以及如何分析日志数据,如何定制Apache日志,如何从日志数据生成统计报表等内容. 如果Apache的安装方式是默认安装,服务器一运行就会有两个日志文件生成.这两个文件是

Apache日志配置参数详细说明

Apache日志按时间分段记录在apache的配置文件httpd.conf中找到ErrorLog logs/error_log及CustomLog logs/access_log commonLinux系统配置方法:将其改为ErrorLog "| /usr/local/apache/bin/rotatelogs /home/logs/www/%Y_%m_%d_error_log 86400 480″CustomLog "| /usr/local/apache/bin/rotatelog

清理apache日志

在不关闭apache服务的情况下清理日志. # cat /dev/null>access_log # cat /dev/null>error_log # cat /dev/null>mod_jk.log 还未测试效果,仅供参考. 清理apache日志

Apache日志轮替规则

linux : |/usr/local/sbin/cronolog /web/apache/logs/%Y%m%daccess_log <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\&quo

Apache日志配置详解(rotatelogs LogFormat)

logs/error_logCustomLog logs/access_log common--默认为以上部分 修改为如下: ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/10.70.25.143:10002.errorlog.%Y%m%d%H 7200 480"CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/10.70.25.143:10002.accesslog.%Y%m%d%

如何在 Linux 上用 SQL 语句来查询 Apache 日志

Linux 有一个显著的特点,在正常情况下,你可以通过日志分析系统日志来了解你的系统中发生了什么,或正在发生什么.的确,系统日志是系统管理员在解决系统和应用问题时最需要的第一手资源.我们将在这篇文章中着重讲解 Apache HTTP web server 生成的 Apache access 日志. 这次,我们会通过另类的途径来分析 Apache access 日志,我们使用的工具是 asql.asql 是一个开源的工具,它能够允许使用者使用 SQL 语句来查询日志,从而通过更加友好的格式展现相同

Apache日志不记录图片文件设置方法和来源日志的配置

Apache日志不记录图片文件设置方法 <FilesMatch "\.(ico|gif|jpg|swf)">SetEnv IMAG 1</FilesMatch>CustomLog logs/access_log combined env=!IMAG Apache日志,如果记录所有访问请求,文件会很大:如果使用日志分析软件,分析结果不一定准确.而且如果不安装cronolog工具来截断日志,会在一个整文件中,这样访问量一高,日志文件所占空间是可观的. 通过在http

apache日志管理

一.apache日志类型 1.apache日志共分为:访问日志,错误日志 2.日志信息的级别 严重程度 等级  说明 1 emerg 系统不可用 2 alert 需要立即引起注意的情况 3 crit  危急情况 4 error 错误信息 5 warn 警告信息 6 notice 需要引起注意的情况 7 info 一般信息 8 debug 由运行于debug模式的程序输出的信息 说明: 如果用户把错误日志设置成warn级别,则严重程度由1~5的所有错误信息都会被记录下来 二.apache访问日志

logresolve - 解析Apache日志中的IP地址为主机名

logresolve是一个解析Apache访问日志中IP地址的后处理程序. 为了使对名称服务器的影响降到最低,logresolve拥有极为自主的内部散列表缓存, 使每个IP值仅仅在第一次从日志文件中读出时才被解析一次. 此程序从标准输入设备上获得需要解析的Apache日志文件, 其中,IP地址必须在每行的开始处,行中其余信息必须以空格分隔. 概要 logresolve [ -s filename ] [ -c ] < access_log > access_log.new 选项 -s file