在 linux 下設定 log 自動輪換功能主要是透過 logrotate 來進行日誌的切換,這個切換的動作會透過每天的 crontab 來執行 可以設定的細節基本上都在 /etc/logrotate.conf 及 /etc/logrotate.d/*.conf 內,基本上只要設定好就不會有什麼問題 細節的參數部份可以參考 man 或網上的其它資料
# man logrotate
範例一:要透過 log rotate 切換 mysql error log & slow log
我的 mysql error 位於 /var/log/mysql/B2BMySQL-s1-error.log, Slow log 在 /var/log/mysql/B2BMySQL-s1-slow.log
/var/log/mysql/B2BMySQL-s1-error.log { create 600 mysql mysql # 切換後檔案的屬性及擁有者 notifempty # 如果日誌檔是空檔則不進行切換動作 daily # 每日切換 maxsize 1000M # 日誌單檔最大1G dateext # 切換後的日誌檔後加上日期 rotate 30 # 保留切換過的30個版本 missingok # 如果日誌檔不存在則繼續及不報錯 compress # 舊的日誌以壓縮方式儲存 delaycompress # 切換後檔案下個間隔才進行壓縮 postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin -u root -pMTIzNDU2 ping &>/dev/null then echo "Flushing Logs" /usr/bin/mysqladmin -u root -pMTIzNDU2 flush-logs fi endscript } /var/log/mysql/B2BMySQL-s1-slow.log { create 600 mysql mysql notifempty daily maxsize 1000M dateext rotate 30 missingok compress delaycompress postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin -u root -pMTIzNDU2 ping &>/dev/null then echo "Flushing Logs" /usr/bin/mysqladmin -u root -pMTIzNDU2 flush-logs fi endscript }
設定好 可執行 logrotate -v 檢查及生效設定
/usr/sbin/logrotate -v /etc/logrotate.conf
若是要強制手動切換可以執行下列指令(此例為生效 mysql logrotate設定
# logrotate --force /etc/logrotate.d/mysql
你必須 登入 才能發表評論。