日志切割
httpd官网有关日志切割的几种方案,帮助文档链接:http://httpd.apache.org/docs/2.4/logs.html;
并发访问量大的网站,每天都会产生容量较大的日志文件,特别是访问日志,所以有必要进行日志切割;
httpd在运行期间进行日志切割,官网给了2个解决方案:日志轮转(Log Rotation)、管道日志(Piped Logs);
本文主要针对管道日志的进行说明,日志轮转的方法可访问上面链接页面做详细了解;
1> 管道日志的特性: Piped log processes are spawned by the parent Apache httpd process, and inherit the userid of that process. This means that piped log programs usually run as root. It is therefore very important to keep the programs simple and secure. 2> 管道日志使用的工具(命令): rotatelogs; One important use of piped logs is to allow log rotation without having to restart the server. The Apache HTTP Server includes a simple program called rotatelogs for this purpose. 备注:可使用'man rotatelogs'查看命令使用帮助; 3> rotatelogs命令描述: Piped logging program to rotate Apache logs 4> rotatelogs命令格式(简化版): rotatelogs [-l] [-c] /PATH/TO/LOG_FILE_NAME {rotation-time|filesize(B|K|M|G)} 常用选项: -l:表示日志轮转的时间以当前系统的时区为准; -c:表示即使日志文件是空的(网页没有访问流量),也做日志轮转; 文件名表示方式: ----------------- /PATH/TO/LOG_FILE_NAME 表示日志存储的位置;其中日志文件名可自定义,比如: access_TIME.log error_TIME.log TIME常用的表达式有: %c :表示时间,最终显示格式为"小时:分钟:秒"; %Y_%m_%d:表示"年_月_日";如果是按天进行轮转日志,则只用此项即可; 轮转后的日志文件名可以这样表示:access-%Y_%m_%d-%c.log;最终显示结果,比如access-2018_06_04-11:44:05.log
下面的2中日志轮转方式选其一即可;
rotation-time表示方式(以时间段为标准进行日志轮转):
——————————————————–
轮转时间以秒为单位,比如以天(24小时)为单位进行日志轮转,则可表示为”86400″;此种方式适合对访问日志使用;
filesize表示方式(以日志文件大小为基准进行日志轮转):
—————————————————-
表示当日志文件的容量达到某个值的时候就会进行轮转,单位为”(B|K|M|G)”,比如当日志文件容量达到5M的时候就轮转,可以表达为’5M’;此种记录方式适合错误日志类型;
举例:
rotatelogs命令对日志文件的切割方式:
1>根据原日志文件的容量大小,比如日志文件达到5M的时候就进行切割;
CustomLog “|/usr/sbin/rotatelogs /var/log/httpd/access-%Y_%m_%d-%c.log 5M” combined
ErrorLog “|/usr/sbin/rotatelogs -l /var/log/httpd/access-%Y_%m_%d-%c.log 5M”
2>根据时间段进行切割,比如24小时(86400秒)为单位进行切割一次;
CustomLog “|/usr/sbin/rotatelogs -l /var/log/httpd/access-%Y_%m_%d-%c.log 86400” combined
CustomLog “|/usr/sbin/rotatelogs -l /var/log/httpd/access-%Y_%m_%d.log 86400” combined