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,下面我们就来详细的讲解一下。

二、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 特点

cronolog主要和Web服务器配置使用,特别是Apache服务器,Apache 默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计。安装cronolog后,可以将日志文件按时间分割,易于管理和分析。下面是与Apache配置的一些指令:

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

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

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

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

下面是具体案例,

/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

/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

四、cronolog 安装

1.安装yum源

1

[[email protected] src]# yum install -y wget vim

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

[[email protected] 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%]

[[email protected] src]# yum install -y wget vim

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

[[email protected] 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

[[email protected] src]# yum install -y ntp

[[email protected] src]# yum install -y ntp

3.时间同步

[[email protected] src]# ntpdate 202.120.2.101

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

[[email protected] 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[[email protected] src]# yum install -y cronolog httpd

[[email protected] src]# yum install -y cronolog httpd

(2).源码安装

[[email protected] src]# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz

[[email protected] src]# tar xf cronolog-1.6.2.tar.gz

[[email protected] src]# cd cronolog-1.6.2

[[email protected] cronolog-1.6.2]# ./configure

[[email protected] cronolog-1.6.2]# make && make install

[[email protected] ~]# which cronolog

/usr/local/sbin/cronolog

好了,到这里我们的cronolog就安装完成了,下面我们来说一下cronolog如何使用。

五、cronolog 使用

(1).基本使用

[[email protected] ~]# 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

[[email protected] ~]# 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 一般是采取管道的方式来工作的,采用如下的形式:

1

[[email protected] ~]# 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文件,将其中的

[[email protected] ~]# 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为日志文件分割方式,即为“年月日”。

[[email protected] ~]# /usr/local/apache2/bin/apachectl restart

(3).下面是效果

[[email protected] ~]# cd /log/www/

[[email protected] 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

时间: 2024-08-09 02:16:56

Linux 日志切割工具cronolog的相关文章

Linux的日志文件管理工具——logrotate(linux日志切割与轮循)

日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事. Linux日志切割 现代的Linux版本都有一个小程序,名为logrotate,用来帮助用户管理日志文件,它以自己的守护进程工作.logrotate周期性地旋转日志文件,可以周期性地把每个日志文件重命名成一个备份名字,然后让它的守护进程开始使用一个日志文件的新的拷贝.

Linux自带 Logrotate 日志切割工具配置详解

Logrotate 程序是一个日志文件管理工具.用于分割日志文件,压缩转存.删除旧的日志文件,并创建新的日志文件,下面就对logrotate日志轮转的记录: 1.1 Logrotate配置文件介绍 Linux系统默认安装logrotate,默认的配置文件: /etc/logrotate.conf /etc/logrotate.d/ logrotate.conf:为主配置文件logrotate.d:为配置相关子系统,用于隔离每个应用配置(Nginx.PHP.Tomcat...)  Logrotat

tomcat 日志文件catalina.out日志切割工具安装

摘要: 有使用tomcat的朋友应该都知道,它会把日志文件都写入到catalina.out这个文件中,这个文件也一直在增长,而且这个日志文件不是很友善.所以我们需要对这个文件进行切割,根据我们的需求显示为一个一个文件,还可以定期清除自己不需要的. 简单安装步骤: 1.我们用一个日志切割的工具cronolog.1.6.2.tar.gz 在下载这个文件之前我们先安装gcc编译器. #yum -y install gcc 下载切割工具,以下的两个连接都可以下载到,任意选个都行. #wget http:

Linux------------logrotate日志切割工具

目录 一.关于日志切割 二.配置文件详解 2.1 配置文件说明 2.2 常见配置参数 2.3 定时任务 三. nginx日志模板 3.1 关于USR1信号解释 转发https://www.cnblogs.com/clsn/p/8428257.html 一.关于日志切割 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事.

日志分割工具——cronolog

使用cronolog可以格式化日志文件的格式,比如按时间分割,易于管理和分析. 1.下载软件 http://cronolog.org/download/index.html 用法见 lighttpd配置文件

awstats 7.4日志分析工具配置

参考博文: http://freeloda.blog.51cto.com/2033581/1346412 http://freeloda.blog.51cto.com/2033581/1346076 http://www.shuijingwanwq.com/2013/07/30/82/ 1.安装apache yum -y install gcc gcc-c++ openssl-devel pcre-devel tar  xf  apr-1.4.6.tar.bz2 cd apr-1.4.6 ./c

tomcat日志切割和定期删除

在tomcat的软件环境中,如果我们任由日志文件无限增长,总有一天会将磁盘占满的(废话).特别是在日志文件增长速度很快的一些情况下,按日志切割日志文件并删除,就是一件很有必要的工作了,以下介绍了切割日志文件的方法. 第1章 系统环境 1.1 操作系统环境 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [[email protected] ~]# uname -r 2.6.32-431.el6.x

Apache日志切割及不记录指定类型日志

1.自带rotatelogs日志切割 ■ rotatelogs 日志轮询说明 ___________________________________________________________ 语法 rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ] 选项 -l    使用本地时间代替GMT时间作为时间基准.注意:在一个改变GMT偏移量(比如夏令时)的环境中 使用-l会导致不可预料的结果. logfile

跨平台日志清理工具 Log-Cutter v2.0.2 正式发布

Log-Cutter 是JessMA开源组织开发的一个简单实用的日志切割清理工具.对于服务器的日常维护来说,日志清理是非常重要的事情,如果残留日志过多则严重浪费磁盘空间同时影响服务的性能.如果用手工方式进行清理,会花费太多时间,并且很多时候难以满足实际要求.例如:如何在每个星期六凌晨3点把超过 2G 大的日志文件进行切割,保留最新的 100M 日志记录? 网上没有发现能满足本座要求的日志切割工具,因此花了一些闲暇时间自己写了一个.由于要在多个平台上使用,为了方便采用 Java 实现.本工具命名为