The log files of the site accumulate with daily access records, and will become larger, especially for large websites.
More and more logs not only affect storage capacity to affect website performance, but also make log analysis difficult for us.
Therefore, I want to standardize the log.
Method One: Use Rotatelogs to implement log rotation
Rotatelogs is a simple program that works with the Apache pipeline Log feature.
PS : Pipeline logging feature
Apache provides the ability to send logs directly to another program, rather than writing them to a file. This greatly enhances the ability to process logs. The program can be any program, such as log analysis, compression log and so on. To implement an operation that writes logs to a pipeline, simply replace the contents of the log file portion of the configuration with the "| Program name", for example:
# compressed logs$ customlog "|/usr/bin/gzip-c >>/var/log/access_log.gz" common
Rotatelogs Grammar
Rotatelogs [-l] logfile [rotationtime [offset]] | [Filesizem]
LogFile it plus the reference name is the log file name. If logfile contains "%", it is treated as a format string for strftime (), otherwise it is automatically appended with the ". nnnnnnnnnn" suffix in seconds. Both formats represent the time when the new log began to be used. Rotationtime the interval of time in seconds that the log file is scrolled. The number of minutes to offset the time difference from UTC. If omitted, it is assumed to be "0" and UTC time is used. For example, to specify local time for a region with a UTC difference of "-5 hours", this parameter should be "300". FILESIZEM Specifies to scroll with the size of the Filesizem file instead of scrolling by time or slack. Examples of settings under Windows are as follows:
#限制错误日志文件为 1M
errorlog "|bin/rotatelogs.exe-l logs/error-%y-%m-%d.log 1M"
#每天生成一个错误日志文件
#ErrorLog "|bin/rotatelogs.exe logs/error-%y-%m-%d.log86400 "
#限制访问日志文件为 1M
customlog "|bin/rotatelogs.exe-l logs/access-%y-%m-%d.log 1M" Common
#每天生成一个访问日志文件
#CustomLog "|bin/rotatelogs.exe logs/access-%y-%m-%d.log86400 "Common
The subject (actual use of operation):
Modify /usr/local/apache2/conf/httpd.conf file
put errorlog "Logs/error_log" use # to comment on it and then
Switch
errorlog "|/usr/local/apache2/bin/rotatelogs/usr/local/apache2/logs/%y-%m-%d-error_log 84600 480 "
put customlog "Logs/access_log" Common use # to comment on it and then
Switch
Customlog "|/usr/local/apache2/bin/rotatelogs/usr/local/apache2/logs/%y-%m-%d-access_log 84600 480" combined
and then restart Apache : /usr/local/apache2/bin/apachectl-k Restart
to the logs we can see the change.
-rw-r--r--1 rootroot 13400 Dec 8 15:50 2016-12-08-access_log
-rw-r--r--1 rootroot 3714 Dec 8 15:50 2016-12-08-error_log
can put Access_log and the Error_log deleted, and later logs are generated on a daily basis, the analysis of the log is convenient.
Then you can also delete the old log three days ago by crontab regularly. (Set yourself up below a few days ago.)
Crontab-e
XX * * */usr/bin/find/usr/local/apache2/logs/-name ' *_log '-mtime +3-exec rm-rf {}\;
method Two: Write Shell Script Regular Backup Delete restart log operation via crontab
Vim cron_log.sh
#!/bin/bash# This script run @ 00:00 logs_path= "/usr/local/apache2/logs/" #How much days backup mostdays=3 #Core of script CD $logs _pathdate= ' date +%y-%m-%d-%h ' src_file= "Access.log" tar_file= "access-$DATE. tar.gz" Tar-czf $TAR _file $SRC _ Filerm-f $SRC _filefind/-name "access-*"-type f-mtime + $DAYS-exec rm {} \;/usr/local/apache2/bin/apachectl-k Restar Texit 0;
Add to crontab Periodic execution
Crontab-e
XX * * */usr/local/crontab/cron_log.sh
This article from "Walk on the road in the operation and maintenance of the dog~" blog, reproduced please contact the author!
"Ops small share" finishing Apache logs