logrotate测试_使用 logrotate 对 apache/nginx 日志切割
logrotate?是 linux 系統用來分割日志的系統工具,可以方便將日志按周期(日,周,月)和大小進行分割。
當我們的服務器訪問量比較大時,服務器的 access.log 可能會 G/天的級別增長,而我們希望日志可以按天周月或當日志文件大小達到某個限額時進行分割。
列舉下如何使用它對 apache 或 nginx 日志進行切分:
apache 其實自身已經集成了一個叫rotatelogs的工具,就在 apache 安裝目錄的?bin 下面,可以很方便的進行日志切割規則設置,在你的配置文件或者虛擬主機配置中如下設置
#年月日時分秒 1G大小分割
ErrorLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/error_log_%Y%m%d%H%M%S 1024M"
#年月日 1G大小
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/access_log_%Y%m%d 1024M" common
#年月日 每86400秒 即1天分割一次 480 為時區的偏移量 北京東八區 60 * 8 = 480 s
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/access_log_%Y%m%d 86400 480" common
logrotate工具
/etc/logrotate.conf 主配置文件
# see "man logrotate" for details
# rotate log files weekly 以周作為周期
weekly
# keep 4 weeks worth of backlogs 保留4個分割文件
rotate 4
# create new (empty) log files after rotating old ones 創建新的日志文件
create
# use date as a suffix of the rotated file 以日期為后綴
dateext
# uncomment this if you want your log files compressed 默認不壓縮
#compress
# RPM packages drop log rotation information into this directory 自定義日志分割配置 nginx 的放這就好
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
默認配置如上
/etc/logrotate.d/* 獨立配置文件
我們可以將自定義的日志分割配置放在此處,會被加載進入主配置文件中,比如我們新建一個
/etc/logrotate.d/nginx 配置文件:
nginx的默認日志目錄是 /home/wwwlogs/,默認pid目錄是/usr/local/nginx/logs/nginx.pid 這些需要根據自己的配置做相應調整。
/home/wwwlogs/*.log {
daily #以天為周期分割日志
minsize 1024M #最小 比如每日分割 但如果大小未到 1024M 則不分割
maxsize 2048M #最大 當日志文件超出 2048M 時,即便再下一個時間周期之前 日志也會被分割
rotate 7 #保留七天
missingok #忽略錯誤
notifempty #如果文件為空則不分割 not if empty
dateext #以日期為單位
size 1024M #以大小為限制做日志分割 size 會覆蓋分割周期配置 1024 1024k 1024M 1024G
sharedscripts #開始執行附加的腳本命令 nginx寫日志是按照文件索引進行的 必須重啟服務才能寫入新日志文件
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` #重啟nginx服務
fi
endscript
}
我自己用的,一個日志文件按1G大小分割
/home/wwwlogs/*.log {
rotate 7
missingok
notifempty
dateext
size 1024M
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` #重啟nginx服務
fi
endscript
}
/etc/cron.daily/logrotate 每日任務
cron.daily 是 crond 的一個每日任務,每天凌晨零點執行一次
配置檢測
/usr/sbin/logrotate -dv 來開啟debugger詳情模式來查看配置是否正確
例如檢測全局
/usr/sbin/logrotate -dv /etc/logrotate.conf
檢測nginx配置
/usr/sbin/logrotate -dv /etc/logrotate.d/nginx
強制執行
/usr/sbin/logrotate -fv /etc/logrotate.d/nginx
常見問題
1、because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
日志文件權限問題,在相應的配置中設置如下配置就好 root 組的 root 用戶,自己做相應的權限聲明即可
#su user group
su root root
OK,具體的用法可以 man logrotate 一下!
總結
以上是生活随笔為你收集整理的logrotate测试_使用 logrotate 对 apache/nginx 日志切割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: group_concat 不是可以识别的
- 下一篇: 用户画像标签维度_神策数据钟秉哲:一文了