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文件大小滚动,而不是按照时间或时差滚动。
___________________________________________________________
■ 日志轮询生产环境实际配置
___________________________________________________________
按天轮询的常见生产环境实际配置:
[[email protected]_server]vim /usr/local/apache2/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin [email protected] 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 [[email protected]_server ~]# ls /usr/local/apache2/logs access_log error_log httpd.pid szk5043.top-access_20160402_log szk5043.top-error_log [[email protected]_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以及真实客户端:
[[email protected]_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 #添加此行 [[email protected]_server ~]# apachectl graceful [[email protected]_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"
2.cronolog日志切割
■ 安装rotatelogs 日志切割工具
___________________________________________________________
http://pan.baidu.com/s/1slWp1s5 #百度网盘下载地址 [[email protected]_server src]# tar xf cronolog-1.6.2.tar.gz [[email protected]_server src]# cd cronolog-1.6.2 [[email protected]_server cronolog-1.6.2]# ./configure [[email protected]_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
■ 日志轮询生产环境实际配置
___________________________________________________________
[[email protected]_server ~]#vim /usr/local/apache2/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin [email protected] 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 #新增此行 </VirtualHost> [[email protected]_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.不记录指定类型日志
[[email protected]_server ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf ErrorLog "/usr/local/apache2/logs/szk5043.top-error_log" SetEnvIr 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 [[email protected]_server ~]# apachectl graceful