一、前言

二、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配置的一些指令:

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

下面是具体案例,

1
2
/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
2
3
4
5
6
[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

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

3.时间同步

1
2
[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
[root@node6 src]# yum install -y cronolog httpd

(2).源码安装

1
2
3
4
5
6
7
[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 && make install
[root@localhost ~]# which cronolog
/usr/local/sbin/cronolog

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

五、cronolog 使用

(1).基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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 一般是采取管道的方式来工作的,采用如下的形式:

1
[root@node6 ~]# loggenerator | cronolog log_file_pattern

其中,loggenerator为产生log的程序,而log_file_pattern是日志文件的路径,可以在其中加入cronolog所支持的时间相关的pattern字符,如/www/log/%y/%m/%d/access.log。其pattern为%字符后跟一特殊字符,简述如下:

转义符:

1
2
3
%    %字符
n    换行
t    水平制表符

时间域:

1
2
3
4
5
6
7
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    时区。若时区不能确定,则无意义

日期域:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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文件,将其中的

1
2
3
4
[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).下面是效果

1
2
3
4
5
6
7
[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

六、cronolog 总结

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

本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1346076

Linux 日志切割工具cronolog详解的更多相关文章

  1. Linux文本编译工具VIM详解

    Linux文本编译工具VIM详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.VIM概述 1>.vim简介 >.vi: 全称Visual editor,即文本编辑 ...

  2. [转] Linux抓包工具tcpdump详解

    http://www.ha97.com/4550.html PS:tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使 ...

  3. Linux抓包工具tcpdump详解

    tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使其成为Linux系统下用于网络分析和问题排查的首选工具. tcpd ...

  4. Linux日志文件/var/log详解

    更多内容推荐微信公众号,欢迎关注: 如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时 ...

  5. Linux命令工具 top详解

    Linux命令工具 top详解 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程,即可以通过用户按键来不 ...

  6. (总结)Linux下的暴力密码在线破解工具Hydra详解

    (总结)Linux下的暴力密码在线破解工具Hydra详解 学习了:https://blog.csdn.net/yafeichang/article/details/53502869

  7. Linux 性能测试工具Lmbench详解

    Linux 性能测试工具Lmbench详解 2010-06-04 16:07 佚名 评测中心 字号:T | T Lmbench 是一套简易可移植的,符合ANSI/C 标准为UNIX/POSIX 而制定 ...

  8. Linux自带神器logrotate详解

    Linux自带神器logrotate详解 散尽浮华 运维 3天前   作者:散尽浮华 链接:https://www.cnblogs.com/kevingrace/p/6307298.html 对于 L ...

  9. Linux文件系统的目录结构详解

    Linux文件系统的目录结构详解   一.前 言 文章对Linux下所有目录一一说明,对比较重要的目录加以重点解说,以帮助初学者熟练掌握Linux的目录结构. 二.目 录 1.什么是文件系统 2.文件 ...

随机推荐

  1. Android -- 系统和自定义Notification

    Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...

  2. spring mvc 学习01 搭建

    /** * */ /** * @author Administrator * */ package com.study01.controller; import org.springframework ...

  3. 简单实现http proxy server怎么实现?

    原文:https://blog.csdn.net/dolphin98629/article/details/54599850 简单实现http proxy server怎么实现?

  4. OnBecameVisible和OnBecameInvisible ,OnWillRenderObject

    OnBecameVisible 和 OnBecameInvisible ,OnWillRenderObject 只有在所挂物体(不包括子物体)有render才有效 //可见 private void ...

  5. Express application generator的使用

    首先拷贝express官网的一篇文章: (http://expressjs.com/en/starter/generator.html ) Express application generator ...

  6. [Algorithm] Reverse a linked list

    It helps to understands how recursive calls works. function Node(val) { return { val, next: null }; ...

  7. Linux配置流程

    Linux中常用操作命令 http://www.cnblogs.com/laov/p/3541414.html#zhiling Linux下安装jdk并配置环境变量 http://jingyan.ba ...

  8. ZH奶酪:HTML元素文本溢出显示省略号(...)

    一 单行文本 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow 属性规定当内容溢出元素框时发生的事情. ...

  9. php基础系列:从用户登录处理程序学习mysql扩展基本操作

    用户注册和登录是网站开发最基本的功能模块之一,现在通过登录处理程序代码来学些下php对mysql的基本操作. 本身没有难点,主要是作为开发人员,应该能做到手写这些基本代码,算是自己加强记忆,同时希望能 ...

  10. 如何随机获取数据库不连续ID的数据?

    这个问题的来由是我朋友要为一网站实现一个标签云功能,和我交流后我给出了一个方案,在此略作记录,亦求拍砖. 大概需求这是样的: 在数据库有一张表A如下图: 其中id字段的值未必是连续的,现在我朋友要做的 ...