Linux日志切割工具cronolog详解

一、前言

大家都知道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 as Apache, 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”

下面是具体案例,

/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源

[root@c7-41 ~]# cd /usr/local/src/
[root@c7-41 src]# yum install -y wget vim

[root@c7-41 src]# wget http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
[root@c7-41 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%]

2.安装ntp

[root@c7-41 src]# yum install -y ntp

3.时间同步


[root@c7-41 src]# ntpdate 202.120.2.101
27 May 01:40:44 ntpdate[5136]: no server suitable for synchronization found

ps:

#以下是国内常见的NTP服务器
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学网络中心
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s2c.time.edu.cn 北京邮电大学
s2d.time.edu.cn 西南地区网络中心
s2e.time.edu.cn 西北地区网络中心
s2f.time.edu.cn 东北地区网络中心
s2g.time.edu.cn 华东南地区网络中心
s2h.time.edu.cn 四川大学网络管理中心
s2j.time.edu.cn 大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学

#自动对时
# crontab –e
# 30 3 * * * /usr/sbin/ntpdate -u 202.120.2.101 && hwclock -w //凌晨3点30同步时间并写入硬件

4.安装cronolog

(1).直接用yum安装
[root@c7-41 src]# yum install -y cronolog httpd
[root@c7-41 ~]# which cronolog
/usr/sbin/cronolog

(2).源码安装
[root@c7-41 src]# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
[root@c7-41 src]# tar xf cronolog-1.6.2.tar.gz
[root@c7-41 src]# cd cronolog-1.6.2
[root@c7-41 cronolog-1.6.2]# ./configure
[root@c7-41 cronolog-1.6.2]# make && make install
[root@localhost ~]# which cronolog
/usr/local/sbin/cronolog

五、cronolog 使用

(1)基本使用
[root@c7-41 src]#  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 一般是采取管道的方式来工作的,采用如下的形式:

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文件,将其中的

#刚刚是用yum安装的
[root@c7-41 src]# vim /etc/httpd/conf/httpd.conf

#修改前
CustomLog "logs/access_log" combined

#源码安装的
CustomLog "|/usr/local/sbin/cronolog /log/www/access_%Y%m%d.log" combined

#yum安装的 
CustomLog "|/usr/sbin/cronolog /log/www/access_%Y%m%d.log" combined

#将默认日志: CustomLog "logs/access_log" combined

#修改为:CustomLog "|/usr/local/sbin/cronolog /log/www/access_%Y%m%d.log" combined 即可。其中%Y%m%d为日志文件分割方式,即为“年月日”。
[root@c7-41 ~]# systemctl start httpd
(3).下面是效果
#tab不出来 去刷新web
[root@c7-41 ~]# cd /log/www/
[root@c7-41 www]# ll
total 12
-rw-r--r-- 1 root root 11439 May 27 01:53 access_20200527.log

六、cronolog 总结

好了,到这里我们的cronolog工具就讲解完成了。有博友会问为什么不用apache自带的日志分割工具?apache自带的日志分割工具rotatelogs,据专家说在进行日志切割时容易丢日志,所以这里我们就用cronolog来做日志切割。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云原生解决方案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值