天天看點

Linux 日志切割工具cronolog

一、前言

二、cronolog 簡介

三、cronolog 特點

四、cronolog 安裝

五、cronolog 使用

六、cronolog 總結

注,作業系統 CentOS 6.4 x86_64,軟體版本 cronolog 1.6.2,軟體下載下傳http://cronolog.org/download/index.html。

大家都知道apache伺服器,預設日志檔案是不分割的,一個整檔案既不易于管理,也不易于分析統計。本博文主要講解Web伺服器日志切割工具cronolog,下面我們就來詳細的講解一下。

Welcome to cronolog.org, the home of the cronolog web log rotation program.cronolog is a simple filter program that reads log file entries from standard input and writes each entry to the output file specified by a filename template and the current date and time. When the expanded filename changes, the current file is closed and a new one opened. cronolog is intended to be used in conjunction with a Web server, such asApache, to split the access log into daily or monthly logs.

cronolog 是一個簡單的過濾程式,讀取日志檔案條目從标準輸入和輸出的每個條目并寫入指定的日志檔案的檔案名模闆和目前的日期和時間。當擴充檔案名的變化,目前的檔案是關閉,新開辟的。cronolog 旨在和一個Web伺服器一起使用,如Apache,分割通路日志為每天或每月的日志。

cronolog主要和Web伺服器配置使用,特别是Apache伺服器,Apache 預設日志檔案是不分割的,一個整檔案既不易于管理,也不易于分析統計。安裝cronolog後,可以将日志檔案按時間分割,易于管理和分析。下面是與Apache配置的一些指令:

<code>TransferLog </code><code>"|/usr/sbin/cronolog /web/logs/%Y/%m/%d/access.log"</code>

<code>ErrorLog    </code><code>"|/usr/sbin/cronolog /web/logs/%Y/%m/%d/errors.log"</code>

TransferLog "|/usr/sbin/cronolog /web/logs/%Y/%m/%d/access.log"

ErrorLog    "|/usr/sbin/cronolog /web/logs/%Y/%m/%d/errors.log"

下面是具體案例,

<code>/web/logs/2002/12/31/access</code><code>.log</code><code>/web/logs/2002/12/31/errors</code><code>.log</code>

<code>/web/logs/2003/01/01/access</code><code>.log</code><code>/web/logs/2003/01/01/errors</code><code>.log</code>

/web/logs/2002/12/31/access.log/web/logs/2002/12/31/errors.log

/web/logs/2003/01/01/access.log/web/logs/2003/01/01/errors.log

1.安裝yum源

1

<code>[root@node6 src]</code><code># yum install -y wget vim</code>

<code>[root@node6 src]</code><code># wget http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm</code>

<code>[root@node6 src]</code><code># rpm -ivh epel-release-6-8.noarch.rpm</code>

<code>warning: epel-release-6-8.noarch.rpm: Header V3 RSA</code><code>/SHA256</code> <code>Signature, key ID 0608b895: NOKEY</code>

<code>Preparing...                </code><code>########################################### [100%]</code>

<code>   </code><code>1:epel-release           </code><code>########################################### [100%]</code>

[root@node6 src]# yum install -y wget vim

[root@node6 src]# wget http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm

[root@node6 src]# rpm -ivh epel-release-6-8.noarch.rpm

warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY

Preparing...                ########################################### [100%]

   1:epel-release           ########################################### [100%]

2.安裝ntp

<code>[root@node6 src]</code><code># yum install -y ntp</code>

[root@node6 src]# yum install -y ntp

3.時間同步

<code>[root@node6 src]</code><code># ntpdate 202.120.2.101</code>

<code>28 Dec 17:59:17 ntpdate[1413]: step </code><code>time</code> <code>server 202.120.2.101 offset -25666.776448 sec</code>

[root@node6 src]# ntpdate 202.120.2.101

28 Dec 17:59:17 ntpdate[1413]: step time server 202.120.2.101 offset -25666.776448 sec

4.安裝cronolog

(1).直接用yum安裝

1<code>[root@node6 src]</code><code># yum install -y cronolog httpd</code>

[root@node6 src]# yum install -y cronolog httpd

(2).源碼安裝

[root@node6 src]# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz

[root@node6 src]# tar xf cronolog-1.6.2.tar.gz

[root@node6 src]# cd cronolog-1.6.2

[root@node6 cronolog-1.6.2]# ./configure

[root@node6 cronolog-1.6.2]# make &amp;&amp; make install

[root@localhost ~]# which cronolog

/usr/local/sbin/cronolog

好了,到這裡我們的cronolog就安裝完成了,下面我們來說一下cronolog如何使用。

(1).基本使用

<code>[root@node6 ~]</code><code># cronolog -h</code>

<code>usage: cronolog [OPTIONS] logfile-spec</code>

<code>   </code><code>-H NAME,   --hardlink=NAME maintain a hard link from NAME to current log</code>

<code>   </code><code>-S NAME,   --</code><code>symlink</code><code>=NAME  maintain a symbolic link from NAME to current log</code>

<code>   </code><code>-P NAME,   --prev-</code><code>symlink</code><code>=NAME  maintain a symbolic link from NAME to previous log</code>

<code>   </code><code>-l NAME,   --link=NAME     same as -S</code><code>/--symlink</code>

<code>   </code><code>-h,        --help          print this help, </code><code>then</code> <code>exit</code>

<code>   </code><code>-p PERIOD, --period=PERIOD </code><code>set</code> <code>the rotation period explicitly</code>

<code>   </code><code>-d DELAY,  --delay=DELAY   </code><code>set</code> <code>the rotation period delay</code>

<code>   </code><code>-o,        --once-only     create single output log from template (not rotated)</code>

<code>   </code><code>-x FILE,   --debug=FILE    write debug messages to FILE</code>

<code>                              </code><code>( or to standard error </code><code>if</code> <code>FILE is </code><code>"-"</code><code>)</code>

<code>   </code><code>-a,        --american         American </code><code>date</code> <code>formats</code>

<code>   </code><code>-e,        --european         European </code><code>date</code> <code>formats (default)</code>

<code>   </code><code>-s,    --start-</code><code>time</code><code>=TIME   starting </code><code>time</code>

<code>   </code><code>-z TZ, --</code><code>time</code><code>-zone=TZ      use TZ </code><code>for</code> <code>timezone</code>

<code>   </code><code>-V,      --version         print version number, </code><code>then</code> <code>exit</code>

[root@node6 ~]# cronolog -h

usage: cronolog [OPTIONS] logfile-spec

   -H NAME,   --hardlink=NAME maintain a hard link from NAME to current log

   -S NAME,   --symlink=NAME  maintain a symbolic link from NAME to current log

   -P NAME,   --prev-symlink=NAME  maintain a symbolic link from NAME to previous log

   -l NAME,   --link=NAME     same as -S/--symlink

   -h,        --help          print this help, then exit

   -p PERIOD, --period=PERIOD set the rotation period explicitly

   -d DELAY,  --delay=DELAY   set the rotation period delay

   -o,        --once-only     create single output log from template (not rotated)

   -x FILE,   --debug=FILE    write debug messages to FILE

                              ( or to standard error if FILE is "-")

   -a,        --american         American date formats

   -e,        --european         European date formats (default)

   -s,    --start-time=TIME   starting time

   -z TZ, --time-zone=TZ      use TZ for timezone

   -V,      --version         print version number, then exit

cronolog 一般是采取管道的方式來工作的,采用如下的形式:

[root@node6 ~]# loggenerator | cronolog log_file_pattern

其中,loggenerator為産生log的程式,而log_file_pattern是日志檔案的路徑,可以在其中加入cronolog所支援的時間相關的pattern字元,如/www/log/%y/%m/%d/access.log。其pattern為%字元後跟一特殊字元,簡述如下:

轉義符:  

%    %字元

n    換行

t    水準制表符

時間域:  

H    小時(00..23)

I    小時(01..12)

p    該locale下的AM或PM辨別

M    分鐘(00..59)

S    秒 (00..61, which allows for leap seconds)

X    該locale下時間表示符(e.g.: "15:12:47")

Z    時區。若時區不能确定,則無意義

日期域:  

a    該locale下的工作日簡名(e.g.: Sun..Sat)

A    該locale下的工作日全名(e.g.: Sunday ..  Satur-ay)

b    該locale下的月份簡稱(e.g.: Jan .. Dec)

B    該locale下的月份全稱(e.g.:  January .. December)

c    該locale下的日期和時間(e.g.: "Sun Dec 15  14:12:47 GMT 1996")

d    當月中的天數 (01 .. 31)

j    當年中的天數 (001 .. 366)

m    月數 (01 .. 12)

U    當年中的星期數,以周日作為一周開始,其中第一周為首個含星期天的星期(00..53)

W    當年中的星期數,以星期一作為一周的開始,其中第一周為首個含星期天的星期(00..53)

w    工作日數(0 .. 6, 0表示星期天)

x    該locale下的日期表示(e.g. "13/04/97")

y    兩位數的年份(00 .. 99)

Y    四位數的年份(1970 .. 2038)

(2).結合apache使用

編輯httpd.conf檔案,将其中的

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

将預設日志: CustomLog "logs/access_log" combined

修改為:CustomLog "|/usr/local/sbin/cronolog /log/www/access_%Y%m%d.log" combined 即可。其中%Y%m%d為日志檔案分割方式,即為“年月日”。

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart

(3).下面是效果

[root@localhost ~]# cd /log/www/

[root@localhost www]# ll

total 15072

-rw-r--r-- 1 root root   16028 Dec 26 15:16 access_20131225.log

-rw-r--r-- 1 root root 2406307 Dec 26 23:59 access_20131226.log

-rw-r--r-- 1 root root 8292792 Dec 27 23:59 access_20131227.log

-rw-r--r-- 1 root root 4682211 Dec 28 18:56 access_20131228.log

     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1907668,如需轉載請自行聯系原作者

繼續閱讀