Logrotate滾動openresty日志


一、摘要

Linux服務器上我們用Logrotate來分割歸檔日志文件,結合crond我們可以指定每天在某個時間自動整理日志等文檔。本文主要說明了Centos下Logrotate的使用和配置的方法。

配置文件

Logrotate的配置文件位於 /etc/logrotate.conf
Logrotate的子配置文件位於文件夾 /etc/logrotate.d/ 下,某些軟件,入nginx,會在rpm命令安裝后會把對應的nginx日志分割文件釋放在此,用於定時整理日志文件。

如何使用

我們先試用幫助命令看一下,需要強調的是

  • -d,其翻譯為什么都不做,僅僅是測試,這個參數很大程度方便了我們測試配置文件而不用擔心當前的配置出差錯。
  • -f,強制執行日志滾動操作。

 

如果想測試配置文件

# 測試所有logrotate配置
/usr/sbin/logrotate -d -v /etc/logrotate.conf
 
# 強制執行日志滾動操作,比如nginx
/usr/sbin/logrotate -d -v /etc/logrotate.d/nginx

 

二、實際操作

單文件

確保openresty已經安裝好了,默認的日志文件是 /usr/local/openresty/nginx/logs/host.access.log

我們需要對這個文件,每天做一次切割。

 

編輯文件

vim /etc/logrotate.d/openresty

 

內容如下:

/usr/local/openresty/nginx/logs/host.access.log {
    daily
    rotate 5
    compress
    copytruncate
    dateext
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

 

參數解釋:

weekly #默認每天一個日志歸檔
rotate 5 #最多保存 5 個歸檔
create #日志滾動后創建一個新的日志文件
dateext #歸檔文件名加上日期后綴
compress #歸檔文件是否啟用壓縮
copytruncate  用於還在打開中的日志文件,把當前日志備份並截斷
sharedscripts 作用是在所有的日志文件都輪轉完畢后統一執行一次腳本。
dateext表示備份的日志文件后綴格式為YYYYMMDD
postrotate/endscript   在轉儲以后需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行

 

強制執行回滾

/usr/sbin/logrotate -vf /etc/logrotate.d/openresty

 

查看日志目錄,就會多出一個gz文件

root@ubuntu:/usr/local/openresty/nginx/logs# ll
total 28
drwxr-xr-x  2 root root 4096 Jul 16 06:25 ./
drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../
-rw-r--r--  1 root root  417 Jul 15 17:18 access.log
-rw-r--r--  1 root root  587 Jul 15 18:40 error.log
-rw-r--r--  1 root root    0 Jul 16 06:25 host.access.log
-rw-r--r--  1 root root  206 Jul 15 18:41 host.access.log-20190715.gz
-rw-r--r--  1 root root    5 Jul 16 02:39 nginx.pid

 

理論是每天會執行一次,查看任務計划

root@ubuntu:~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

 

可以發現,每天的任務計划,是6:25執行一次。

如果不想等的話,直接修改系統時間,提前一天

date -s "2019-07-16 06:24:58"

 

注意:等到6:25的時候,日志文件,不會立即產生。我猜測,是還有其他的腳本要執行。

等待2分鍾,就會出現新的文件。

root@ubuntu:/usr/local/openresty/nginx/logs# ll
total 28
drwxr-xr-x  2 root root 4096 Jul 16 06:25 ./
drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../
-rw-r--r--  1 root root  417 Jul 15 17:18 access.log
-rw-r--r--  1 root root  587 Jul 15 18:40 error.log
-rw-r--r--  1 root root    0 Jul 16 06:25 host.access.log
-rw-r--r--  1 root root  206 Jul 15 18:41 host.access.log-20190715.gz
-rw-r--r--  1 root root  201 Jul 15 18:42 host.access.log-20190716.gz
-rw-r--r--  1 root root    5 Jul 16 02:39 nginx.pid

 

多日志文件與通配符

一個配置條目 日志文件 { 配置項 } 不僅僅支持一個日志文件,可以配置多個文件或使用通配符,如

/var/log/httpd/access.log /var/log/httpd/error.log {
....
}
 
# 或
/var/log/httpd/access.log
/var/log/httpd/error.log {
...
}

 

通配符的形式

/var/log/news/* {
...
}
 
/var/log/news/*.log {
...
}
 
/var/log/*/stdout.log {
...
}
 
/var/log/*/*.log {
...
}

 

不僅文件名處可以用通配符,目錄處也能用通配符。例如最后那個配置 /var/log/*/*.log {...} 將會對 /var/log/ 下所有目錄中的 *.log 文件產生效果。比如下面那樣的文件

/var/log/aa/x.log
/var/log/bb/y.log
/var/log/cc/z.log

 

針對多個日志文件時,歸檔文件也會生成在相應日志所在目錄中。有了多日志文件與通配符的支持,能夠通過一個配置對系統中眾多日志文件采取一致的行動。

 

舉例

查看 /usr/local/openresty/nginx/logs 目錄,有3個日志文件,分別是 access.log,error.log,host.access.log

host.access.log 已經做了滾動,現在需要對另外個日志文件,也做一下滾動。

有2種寫法,先看第一種:通配符的形式

/usr/local/openresty/nginx/logs/*.log {
    daily
    rotate 5
    compress
    copytruncate
    dateext
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

 

第二種:多個文件形式

/usr/local/openresty/nginx/logs/access.log
/usr/local/openresty/nginx/logs/error.log
/usr/local/openresty/nginx/logs/host.access.log {
    daily
    rotate 5
    compress
    copytruncate
    dateext
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

 

這2種寫法,效果是一樣的。

先強制回滾一下

/usr/sbin/logrotate -vf /etc/logrotate.d/openresty

 

修改日期

date -s "2019-07-17 06:24:58"

 

等待2分鍾,再次查看日志目錄,效果如下:

root@ubuntu:/etc/logrotate.d# ll /usr/local/openresty/nginx/logs/
total 36
drwxr-xr-x  2 root root 4096 Jul 18 06:25 ./
drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../
-rw-r--r--  1 root root    0 Jul 18 06:25 access.log
-rw-r--r--  1 root root  229 Jul 15 17:18 access.log-20190718.gz
-rw-r--r--  1 root root    0 Jul 18 06:25 error.log
-rw-r--r--  1 root root  382 Jul 16 18:19 error.log-20190718.gz
-rw-r--r--  1 root root    0 Jul 18 06:25 host.access.log
-rw-r--r--  1 root root  206 Jul 15 18:41 host.access.log-20190715.gz
-rw-r--r--  1 root root  201 Jul 15 18:42 host.access.log-20190716.gz-rw-r--r--  1 root root   20 Jul 17 06:25 host.access.log-20190718.gz
-rw-r--r--  1 root root    5 Jul 16 17:48 nginx.pid

 

本文參考鏈接:

https://blog.phpgao.com/logrotate_conf.html

https://yanbin.blog/linux-config-log-ratation-logrotate/#more-8826

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM