- 1. 关于本文
- 本文将以cronolog 1.6.2、apache 2.2.6为例,以CentOS 5为平台,讲述cronolog的安装和设置。
- 2. 关于cronolog
- cronolog是一个简单的过滤程序,它从标准输入设备读入日志记录,并把这些记录写入到输出文件集,输出文件的名字由一个文件名模板和当前的日期时间组成。cronolog通常与web服务器一起使用,例如apache,用来安全地对日志文件按日期、月或其它特定的区间进行分割。
- 3. 安装cronolog
- 3.1 下载最新稳定发行版(GA)的cronolog
- 访问cronolog网站http://cronolog.org/download/index.html下载最新稳定发行版的cronolog源码包。本文使用的是1.6.2版本,在linux系统下用下面的命令下载:
- wgethttp://cronolog.org/download/cronolog-1.6.2.tar.gz
- 3.2 解压缩下载的源码包
- 首先建立一个工作目录( 笔者建议的目录为/usr/local/src/cronolog ) :
- mkdir -p /usr/local/src/cronolog
- 将下载的源码包移至工作目录:
- mv cronolog-1.6.2.tar.gz /usr/local/src/cronolog
- 进入工作目录并用tar命令解压源码包:
- cd /usr/local/src/cronolog
- tar zxvf cronolog-1.6.2.tar.gz
- 命令执行结束后,当前工作目录下将生成一个新的子目录cronolog-1.6.2,此目录下即为cronolog的源码文件。
- 3.3 配置Makefile文件
- 进入cronolog源码目录:
- cd cronolog-1.6.2
- 执行下面的命令可查看可配置选项:
- ./configure --help
- 本文使用的配置命令如下:
- CC=gcc CFLAGS="-O3" ./configure --prefix=/usr/local/cronolog
- 配置选项说明:
- CC:C编译器的名称(用于运行configure),本文示例为gcc
- CFLAGS:C编译器的标志(用于运行configure),本文示例为-O3,指定优化级别为3
- --prefix:指定安装目录,本文示例为/usr/local/cronolog
- 3.4 编译源代码
- 执行下面的命令编译源代码:
- make
- 3.5 安装
- 执行下面的命令安装cronolog 到目标路径:
- make install
- 3.6 目录结构
- 安装完毕后,将在先前指定的目标路径中生成下列目录:
- ./info cronolog信息文件(.info)目录
- ./man cronolog帮助文件(man)目录
- ./sbin cronolog二进制文件目录
- 4. cronolog的使用
- cronolog通常以管道方式作为日志过滤程序在应用的配置文件中调用。
- 直接用法是:
- /path/to/cronolog [OPTIONS] logfile-spec
- 其中:
- OPTIONS:cronolog的选项,可通过下面示例中的-h 或 --help选项查看,此处不再介绍。
- 本文获取帮助信息示例:
- /usr/local/cronolog/sbin/cronolog -h
- 或:
- /usr/local/cronolog/sbin/cronolog --help
- logfile-spec: 是描述输出的日志文件名的模板,每一个无前导%的字符都是文件名的组成部分,%后面跟一个字符为日期和时间格式串,将被下表列出的它们代表的实际字串所替换。
- 特殊格式串:
- %% %字符
- %n 新行
- %t tab字符
- 时间格式串:
- %H 24小时制小时(00..23)
- %I 12小时制小时(01..12)
- %p 本地AM/PM指示符
- %M 分钟(00..59)
- %S 秒(00..61)
- %X 本地时间(e.g.: "15:12:47")
- %Z 时区 (e.g. GMT),如果不能检测出时区,值为空
- 日期格式串:
- %a 本地简短星期名(e.g.: Sun..Sat)
- %A 本地完整星期名(e.g.: Sunday .. Saturday)
- %b 本地简短月名(e.g.: Jan .. Dec)
- %B 本地完整月名(e.g.: January .. December)
- %c 本地日期与时间(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 本地日期 (e.g. 今天在北京是: "15/12/96")
- %y 不带世纪的年(00 .. 99)
- %Y 带世纪的年(1970 .. 2038)
- 下面是在apache中的用法:
- CustomLog "|/path/to/cronolog [OPTIONS] logfile-spec" [format]
- OPTIONS、logfile-spec同上面的直接用法,format为apache配置指令CustomLog的日志格式参数。
- 下面是本文的示例:
- 修改apache配置文件,本文示例为/usr/local/apache-2.2.6/conf/httpd.conf:
- vi /usr/local/apache-2.2.6/conf/httpd.conf
- 按下面的提示进行修改:
- 将CustomLog指令,本文示例为
- CustomLog logs/access_log common
- 更改为:
- CustomLog "|/usr/local/cronolog/sbin/cronolog /usr/local/apache-2.2.6/logs/access_log.%Y%m%d" combined
- 指令解释:
- /usr/local/cronolog/sbin/cronolog 为cronolog二进制文件绝对路径
- /usr/local/apache-2.2.6/logs/access_log.%Y%m%d 为输出日志文件名模板,将按天生成类似下面文件名的日志文件/usr/local/apache-2.2.6/logs/access_log.20080301。
- combined 为apache日志的格式名。
- 按你的实际情况修改完毕后重启apache即可。
- 5. 结束语
- 至此,cronolog基本安装配置完毕。希望本文能对初学者有所帮助。
- 使用cronolog可以格式化日志文件的格式,比如按时间分割,易于管理和分析。
- cronolog的安装配置非常简单,简要说明如下:
- 1.下载软件
- http://cronolog.org/download/index.html
- 2.解压缩
- gzip -d cronolog-1.6.2.tar.gz
- tar xf cronolog-1.6.2.tar
- 2.进入相应的目录./configure
- 3.make
- 4.make install
- 5.修改apache配置文件
- 以下是我的安装日志,供大家参考:
- 考:
- [root@eygle opt]# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
- --08:05:12-- http://cronolog.org/download/cronolog-1.6.2.tar.gz
- => `cronolog-1.6.2.tar.gz'
- Resolving cronolog.org... done.
- Connecting to cronolog.org[217.160.212.212]:80... connected.
- HTTP request sent, awaiting response... 200 OK
- Length: 133,591 [application/x-gzip]
- 100%[==================================>] 133,591 26.23K/s ETA 00:00
- 08:05:19 (26.23 KB/s) - `cronolog-1.6.2.tar.gz' saved [133591/133591]
- [root@eygle opt]# gzip -d cronolog-1.6.2.tar.gz
- [root@eygle opt]# tar xf cronolog-1.6.2.tar
- [root@eygle opt]# cd cronolog-1.6.2
- [root@eygle cronolog-1.6.2]# ls
- aclocal.m4 config.cache configure cronolog.spec install-sh Makefile.am mkinstalldirs src
- AUTHORS config.log configure.in doc lib Makefile.in NEWS testsuite
- ChangeLog config.status COPYING INSTALL Makefile missing README TODO
- [root@eygle cronolog-1.6.2]# ./configure
- loading cache ./config.cache
- checking for a BSD compatible install... (cached) /usr/bin/install -c
- checking whether build environment is sane... yes
- checking whether make sets ${MAKE}... (cached) yes
- checking for working aclocal... found
- checking for working autoconf... found
- checking for working automake... found
- checking for working autoheader... found
- checking for working makeinfo... found
- checking for gcc... (cached) gcc
- checking whether the C compiler (gcc ) works... yes
- checking whether the C compiler (gcc ) is a cross-compiler... no
- checking whether we are using GNU C... (cached) yes
- checking whether gcc accepts -g... (cached) yes
- checking for a BSD compatible install... /usr/bin/install -c
- checking whether ln -s works... (cached) yes
- checking for ranlib... (cached) ranlib
- checking for perl... (cached) /usr/bin/perl
- checking how to run the C preprocessor... (cached) gcc -E
- checking for ANSI C header files... (cached) yes
- checking whether stat file-mode macros are broken... (cached) no
- checking whether time.h and sys/time.h may both be included... (cached) yes
- checking whether struct tm is in sys/time.h or time.h... (cached) time.h
- checking for tm_zone in struct tm... (cached) yes
- checking for fcntl.h... (cached) yes
- checking for limits.h... (cached) yes
- checking for unistd.h... (cached) yes
- checking for working const... (cached) yes
- checking for size_t... (cached) yes
- checking whether struct tm is in sys/time.h or time.h... (cached) time.h
- checking for strftime... (cached) yes
- checking for vprintf... (cached) yes
- checking for mkdir... (cached) yes
- checking for mktime... (cached) yes
- checking for putenv... (cached) yes
- checking for strptime... (cached) yes
- checking for localtime_r... (cached) yes
- creating ./config.status
- creating Makefile
- creating lib/Makefile
- creating src/Makefile
- creating doc/Makefile
- creating testsuite/Makefile
- creating src/cronosplit
- [root@eygle cronolog-1.6.2]# make
- Making all in lib
- make[1]: Entering directory `/opt/cronolog-1.6.2/lib'
- make[1]: Nothing to be done for `all'.
- make[1]: Leaving directory `/opt/cronolog-1.6.2/lib'
- Making all in src
- make[1]: Entering directory `/opt/cronolog-1.6.2/src'
- make[1]: Nothing to be done for `all'.
- make[1]: Leaving directory `/opt/cronolog-1.6.2/src'
- Making all in doc
- make[1]: Entering directory `/opt/cronolog-1.6.2/doc'
- make[1]: Nothing to be done for `all'.
- make[1]: Leaving directory `/opt/cronolog-1.6.2/doc'
- Making all in testsuite
- make[1]: Entering directory `/opt/cronolog-1.6.2/testsuite'
- make[1]: Nothing to be done for `all'.
- make[1]: Leaving directory `/opt/cronolog-1.6.2/testsuite'
- make[1]: Entering directory `/opt/cronolog-1.6.2'
- make[1]: Nothing to be done for `all-am'.
- make[1]: Leaving directory `/opt/cronolog-1.6.2'
- [root@eygle cronolog-1.6.2]# make install
- Making install in lib
- make[1]: Entering directory `/opt/cronolog-1.6.2/lib'
- make[2]: Entering directory `/opt/cronolog-1.6.2/lib'
- make[2]: Nothing to be done for `install-exec-am'.
- make[2]: Nothing to be done for `install-data-am'.
- make[2]: Leaving directory `/opt/cronolog-1.6.2/lib'
- make[1]: Leaving directory `/opt/cronolog-1.6.2/lib'
- Making install in src
- make[1]: Entering directory `/opt/cronolog-1.6.2/src'
- make[2]: Entering directory `/opt/cronolog-1.6.2/src'
- /bin/sh ../mkinstalldirs /usr/local/sbin
- /usr/bin/install -c cronolog /usr/local/sbin/cronolog
- /bin/sh ../mkinstalldirs /usr/local/sbin
- /usr/bin/install -c cronosplit /usr/local/sbin/cronosplit
- make[2]: Nothing to be done for `install-data-am'.
- make[2]: Leaving directory `/opt/cronolog-1.6.2/src'
- make[1]: Leaving directory `/opt/cronolog-1.6.2/src'
- Making install in doc
- make[1]: Entering directory `/opt/cronolog-1.6.2/doc'
- make[2]: Entering directory `/opt/cronolog-1.6.2/doc'
- make[2]: Nothing to be done for `install-exec-am'.
- /bin/sh ../mkinstalldirs /usr/local/info
- /usr/bin/install -c -m 644 ./cronolog.info /usr/local/info/cronolog.info
- install-info --info-dir=/usr/local/info /usr/local/info/cronolog.info
- make install-man1
- make[3]: Entering directory `/opt/cronolog-1.6.2/doc'
- /bin/sh ../mkinstalldirs /usr/local/man/man1
- /usr/bin/install -c -m 644 ./cronolog.1m /usr/local/man/man1/cronolog.1m
- /usr/bin/install -c -m 644 ./cronosplit.1m /usr/local/man/man1/cronosplit.1m
- make[3]: Leaving directory `/opt/cronolog-1.6.2/doc'
- make[2]: Leaving directory `/opt/cronolog-1.6.2/doc'
- make[1]: Leaving directory `/opt/cronolog-1.6.2/doc'
- Making install in testsuite
- make[1]: Entering directory `/opt/cronolog-1.6.2/testsuite'
- make[2]: Entering directory `/opt/cronolog-1.6.2/testsuite'
- make[2]: Nothing to be done for `install-exec-am'.
- make[2]: Nothing to be done for `install-data-am'.
- make[2]: Leaving directory `/opt/cronolog-1.6.2/testsuite'
- make[1]: Leaving directory `/opt/cronolog-1.6.2/testsuite'
- make[1]: Entering directory `/opt/cronolog-1.6.2'
- make[2]: Entering directory `/opt/cronolog-1.6.2'
- make[2]: Nothing to be done for `install-exec-am'.
- make[2]: Nothing to be done for `install-data-am'.
- make[2]: Leaving directory `/opt/cronolog-1.6.2'
- make[1]: Leaving directory `/opt/cronolog-1.6.2'
- [root@eygle cronolog-1.6.2]# which cronolog
- /usr/local/sbin/cronolog
- 安装完成以后需要对apache进行适当配置,修改httpd.conf文件,主要注意以下几点:
- 1.自定义日志格式
- CustomLog "|/usr/local/sbin/cronolog /opt/apache/logs/access_log.%Y%m%d" combined
- 2.如果存在多个虚拟站点
- 可以考虑在VirtualHost进行相应设置
- ServerAdmin [email protected]
- DocumentRoot /www/docs/dummy-host.example.com
- ServerName dummy-host.example.com
- ErrorLog logs/dummy-host.example.com-error_log
- CustomLog logs/dummy-host.example.com-access_log common
- 我的www.eygle.com存在多个子站点,但是都使用了同一个日志文件配置后生成日志文件的效果:
- [root@eygle logs]# ls -l access_log.20041226
- -rw-r--r-- 1 root root 110425 Dec 26 09:10 access_log.20041226