Apache日志切割及不记录指定类型日志

回复 收藏
本帖最后由 清茶一杯 于 2016-4-24 17:15 编辑

1.自带rotatelogs日志切割
■ rotatelogs 日志轮询说明
___________________________________________________________
语法
rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ]
选项
-l    使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中
使用-l会导致不可预料的结果。
logfile  它加上基准名就是日志文件名。如果logfile中包含"%",则它会被视为用于strftime()的格式字符串; 否则它会被自动加上以秒为单位的".nnnnnnnnnn"后缀。这两种格式都表示新的日志开始使用的时间。
rotationtime 日志文件滚动的以秒为单位的间隔时间。  
offset  相对于UTC的时差的分钟数。如果省略,则假定为"0"并使用UTC时间。比如,要指定UTC时差为
"-5小时"的地区的当地时间,则此参数应为"-300"。
filesizeM  指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。

___________________________________________________________
■ 日志轮询生产环境实际配置
___________________________________________________________
按天轮询的常见生产环境实际配置:

[root@Web_server]vim /usr/local/apache2/conf/extra/httpd-vhosts.conf


    ServerAdmin szk5043@foxmail.com
    DocumentRoot "/data/www"
    ServerName szk5043.top
   # ServerAlias www.dummy-host.example.com
    ErrorLog "/usr/local/apache2/logs/szk5043.top-error_log"
    CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/szk5043.top-access_%Y%m%d_log 86400" combined

[root@Web_server ~]# ls /usr/local/apache2/logs
access_log  error_log  httpd.pid  szk5043.top-access_20160402_log  szk5043.top-error_log

[root@Web_server ~]# cat /usr/local/apache2/logs/szk5043.top-access_20160402_log
127.0.0.1 - - [02/Apr/2016:16:10:17 +0800] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.2.31 (Unix) PHP/5.5.31 (internal dummy connection)"
127.0.0.1 - - [02/Apr/2016:16:10:17 +0800] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.2.31 (Unix)

___________________________________________________________
日志记录代理IP以及真实客户端:
[root@Web_server ~]# vim /usr/local/apache2/conf/httpd.conf
    #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined       #搜索LogFormat关键字,注释原有配置
    LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined    #添加此行
[root@Web_server ~]# apachectl graceful

[root@Web_server ~]# tail -2 /usr/local/apache2/logs/szk5043.top-access_20160402_log
127.0.0.1 - - - [02/Apr/2016:16:20:32 +0800] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.2.31 (Unix) PHP/5.5.31 (internal dummy connection)"
112.65.11.52 - - - [02/Apr/2016:16:21:09 +0800] "GET /favicon.ico HTTP/1.1" 200 5558 "http://115.28.241.168/admin.php" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
http://file:///C:/Users/szk/AppData/Local/YNote/data/qqC911E522586952C3F413B8CDDA09F207/46656513fe2b445780d62f1329abcefa/clipboard.png
clipboard.png


2.cronolog日志切割

■ 安装rotatelogs 日志切割工具
___________________________________________________________


http://pan.baidu.com/s/1slWp1s5     #百度网盘下载地址
[root@Web_server src]# tar xf cronolog-1.6.2.tar.gz
[root@Web_server src]# cd cronolog-1.6.2
[root@Web_server cronolog-1.6.2]# ./configure

[root@Web_server cronolog-1.6.2]# make && make install

■ cronolog日志切割配置说明
___________________________________________________________
#按小时切割
CustomLog "|/usr/local/sbin/cronolog   /usr/local/apache2/logs/access_szk5043.top_%Y%m%d%H.log" combined

#按天切割(日志不会自动覆盖)
CustomLog "|/usr/local/sbin/cronolog   /usr/local/apache2/logs/access_szk5043.top_%Y%m%d.log" combined

#按周建议目录,按天轮询
CustomLog "|/usr/local/sbin/cronolog   /usr/local/apache2/logs/%w/access_szk5043.top_%Y%m%d.log" combined


■ 日志轮询生产环境实际配置
___________________________________________________________
[root@Web_server ~]#vim /usr/local/apache2/conf/extra/httpd-vhosts.conf


    ServerAdmin szk5043@foxmail.com
    DocumentRoot "/data/www"
    ServerName szk5043.top
   # ServerAlias www.dummy-host.example.com
    ErrorLog "/usr/local/apache2/logs/szk5043.top-error_log"
   #CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/szk5043.top-access_%Y%m%d_log 86400" combined    #注释前面配置
    CustomLog "|/usr/local/sbin/cronolog   /usr/local/apache2/logs/access_szk5043.top_%Y%m%d%H.log" combined    #新增此行



[root@Web_server ~]# cat /usr/local/apache2/logs/access_szk5043.top_2016040217.log
127.0.0.1 - - - [02/Apr/2016:17:12:29 +0800] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.2.31 (Unix) PHP/5.5.31 (internal dummy connection)"

3.不记录指定类型日志
[root@Web_server ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
    ErrorLog "/usr/local/apache2/logs/szk5043.top-error_log"
    SetEnvIf Request_URI ".*\.gif$" image-request
    SetEnvIf Request_URI ".*\.jpg$" image-request    SetEnvIf Request_URI ".*\.png$" image-request
    SetEnvIf Request_URI ".*\.bmp$" image-request    SetEnvIf Request_URI ".*\.swf$" image-request
    SetEnvIf Request_URI ".*\.js$" image-request
    SetEnvIf Request_URI ".*\.css$" image-request
   CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/szk5043.top-access_%Y%m%d_log 86400" combined

[root@Web_server ~]# apachectl graceful




2016-04-02 17:47 举报
已邀请:
0

乐橙306

赞同来自:

参考文档
http://apache.chinahtml.com/logs.html


按日切割
修改配置文件  httpd.conf  保存  重启
#CustomLog logs/access_log combined
CustomLog "| /usr/sbin/rotatelogs -l /var/log/httpd/access_%Y%m%d.log 86400" combined
0

fishboy84

赞同来自:

SetEnvIr Request_URI ".*\.gif$" image-request  更新一下 If

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: