1. 程式人生 > >Linux中利用logrotate來對log檔案進行轉儲

Linux中利用logrotate來對log檔案進行轉儲

使用logrotate對日誌檔案轉儲,按時或者按大小。

logrotate簡介

NAME
       logrotate - rotates, compresses, and mails system logs

SYNOPSIS
       logrotate [-dv] [-f|--force] [-s|--state file] config_file ..

DESCRIPTION
       logrotate  is  designed  to  ease  administration of systems that generate large numbers of
log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. Normally, logrotate is run as a daily cron job. It will not modify a log multiple times in one day unless the
criterium for that log is based on the log's size and logrotate is being run multiple times each day, or unless the -f or -force option is used. Any number of config files may be given on the command line. Later config files may override the options given in earlier files, so the
order in which the logrotate config files are listed is important. Normally, a single config file which includes any other config files which are needed should be used. See below for more information on how to use the include directive to accomplish this. If a directory is given on the command line, every file in that directory is used as a con- fig file. If no command line arguments are given, logrotate will print version and copyright information, along with a short usage summary. If any errors occur while rotating logs, logrotate will exit with non-zero status.

使用需求

在啟動flume時候會將啟動日誌和接下來的執行日誌收集到日誌檔案中/var/log/flume/agent.out,注意這裡的這個日誌檔案不是log4j配置的日誌檔案。

/usr/hdp/current/flume-server/bin/flume-ng agent --name agent --conf /etc/flume/conf/agent --conf-file /etc/flume/conf/agent/flume.conf -Dflume.monitoring.type=org.apache.hadoop.metrics2.sink.flume.FlumeTimelineMetricsSink -Dflume.monitoring.node=10.254.100.198:6188 > /var/log/flume/agent.out 2>&1

遇到異常情況該日誌檔案會變的非常大,產生系統隱患,那麼我們就需要控制這個檔案的大小,最好可以像log4j一樣可以定時的或者按照大小來“轉儲”日誌檔案,這就可以使用logrotate這個命令實現。

主流Linux發行版上都預設安裝有logrotate包,如果出於某種原因,logrotate沒有出現在裡頭,可以使用apt-get或yum命令來安裝。

樣例配置

基於上面的需求,我們在/etc/logrotate.d/下面建立自定義的配置檔案:

vi /etc/logrotate.d/flume

/var/log/flume/agent.out {
    size 100M
    create
    start 1
    rotate 5
    compress
    copytruncate
    missingok
}

說明一下(詳情請參見man logrotate):

  1. size:Log files are rotated when they grow bigger than size bytes.
  2. create:Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated).
  3. start:This is the number to use as the base for rotation. For example, if you specify 0, the logs will be created with a.0 extension as they are rotated from the original log files.
  4. rotate:Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated.
  5. compress:Old versions of log files are compressed with gzip(1) by default. See also nocompress.
  6. copytruncate:Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one.
  7. missingok:If the log file is missing, go on to the next one without issuing an error message. See also nomissingok.

效果

預設情況下,logrotate會每天自動執行一次:
在/etc/cron.daily目錄下有logrotate執行的指令碼(通過crontab程式每天執行一次)

vi /etc/cron.daily/logrotate

如果配置完想立即測試看一下效果,可以使用logrotate的強制執行的命令:

/usr/sbin/logrotate -vf /etc/logrotate.conf