对于服务器的日常维护来说,日志清理是非常重要的事情,如果残留日志过多则严重浪费磁盘空间同时影响服务的性能。如果用手工方式进行清理,会花费太多时间,并且很多时候难以满足实际要求。例如:如何在每个星期六凌晨3点把超过2G大的日志文件进行切割,保留最新的100M日志记录?
网上没有发现能满足本座要求的日志切割工具,因此花了一些闲暇时间自己写了一个。由于要在多个平台上使用,为了方便采用 Java 实现。本工具命名为 LogCutter,主要有以下特点:
- 支持 Linux、Mac 和 Windows 等所有常见操作系统平台
- 支持命令行交互式运行
- 支持后台非交互式运行(Linux/MAC 下使用 daemon 进程实现,Windows 用系统 Service 实现)
- 支持两种日志清理方式(删除日志文件或切割日志文件)
- 支持对 GB18030、UTF-8、UTF-16LE、UTF-16BE 等常用日志文件类型进行切割(不会发生切掉半个字符的情况)
- 高度可配置(程序执行周期、要删除的日志文件过期时间、要切割的日志文件阀值和保留大小等均可配置
- 使用方法:
*******************************************************
**** LogCutter - Bruce Liang, all rights reserved. ****
*******************************************************
一、启动方式
--------------------------------------------------
1) Windows
A) 前台运行: > ./run.bat
B) 后台运行: > ./LogCutter.exe {
-install-demand (安装手动启动服务)
-install-auto (安装自动启动服务)
-start (启动服务)
-stop (停止服务)
-status (查看服务状态)
}
*** 注 ***
@ LogCutter.exe 以 Windows 系统服务的方式运行,安装好后也可以通过 Windows 服务管理器进行管理
@ LogCutter.exe 依赖 JRE 1.6, 因此必须在注册表或环境变量中设置 %JAVA_HOME%
2) Linux / Unix
A) 前台运行: $ ./run.sh
B) 后台运行: $ ./run.sh -d
*** 注 ***
@ 可以设置为开机时自动后台运行,方法如下:
$ vi /etc/rc.d/rc.local
( 加入: ${YOUR_LOGCUTTER_PATH}/run.sh -d )
--------------------------------------------------
二、配置文件
--------------------------------------------------
1) 程序配置文件: ./conf/config.xml
2) 日志配置文件: ./conf/log4j.properties (默认)
--------------------------------------------------
三、环境要求
--------------------------------------------------
1) Java 版 本: JRE 1.6 以上
2) 依赖程序包: dom4j、log4j、juniversalchardet
--------------------------------------------------
- run.sh (Mac 平台)
#!/bin/bash
JAVA_HOME="/Library/Java/Home"
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
JAVA_BIN=$JAVA_HOME/bin
JAVA=$JAVA_BIN/java
APP_PATH="/Users/Kingfisher/MyWork/Java/LogCutter"
APP_CLASSPATH=$APP_PATH/classes
APP_LIBPATH=$APP_PATH/lib
APP_LIBS=$APP_LIBPATH/dom4j-1.6.1.jar:$APP_LIBPATH/log4j-1.2.16.jar:$APP_LIBPATH/juniversalchardet-1.0.3.jar
APP_CONFPATH=$APP_PATH/conf
APP_CONFFILE=$APP_CONFPATH/config.xml
APP_MAIN_CLASS=com.bruce.main.LogCutter
DAEMON='-d'
CMD="$JAVA -cp $CLASSPATH:$APP_CLASSPATH:$APP_LIBS $APP_MAIN_CLASS -f $APP_CONFFILE"
if [ $# -eq 0 ]
then
$CMD
elif [ "$#" -eq 1 -a "$1" = "$DAEMON" ]
then
$CMD &
else
echo "Usage: $0 [ $DAEMON ]"
exit 1
fi
- 交互式运行操作演示:
[Kingfisher@Bruce-mbp LogCutter] $ pwd
/Users/Kingfisher/MyWork/Java/LogCutter
[Kingfisher@Bruce-mbp LogCutter] $ ./run.sh
command line usage
------------------------------------------------------------
HELP : Show help
JOBS : Show jobs status
CFG : Show configuration summary
RUN : Schedule jobs manually
!Q : Shutdown application
? : About me
------------------------------------------------------------
> help
command line usage
------------------------------------------------------------
HELP : Show help
JOBS : Show jobs status
CFG : Show configuration summary
RUN : Schedule jobs manually
!Q : Shutdown application
? : About me
------------------------------------------------------------
> jobs
jobs summary (total: 2, active: 0)
------------------------------------------------------------
1. DelFileRunner@334936591 [ Idle ]
2. CutFileRunner@724646150 [ Idle ]
------------------------------------------------------------
> cfg
configuration summary
------------------------------------------------------------
[global]
start-check-delay : 1 hours
check-interval : 24 hours
log4j-config-file : /Users/Kingfisher/MyWork/Java/LogCutter/classes/../conf/log4j.properties
lock-file : /Users/Kingfisher/MyWork/Java/LogCutter/classes/../LogCutter.lock
[delete-files] (expire: 30 days)
1. /opt/LogCutter/logs/LogCutter.log*
2. /opt/tomcat6-logs/*.log
3. /opt/tomcat7-logs/*.log
[cut-files] (threshold: 30960 KBs, reserve: 1024 KBs)
1. /opt/apache-logs/*_log
2. /opt/nginx-logs/*.log
3. /opt/tomcat6-logs/catalina.out
4. /opt/tomcat7-logs/catalina.out
5. /var/lib/mysql/*.err
------------------------------------------------------------
> run
manual jobs are scheduled !
> ?
LogCutter 1.0.2 - Bruce Liang, all rights reserved.
------------------------------------------------------------
Description : schedule to DELETE and CUT text log files automatically or manually.
Support : GB18030, UTF-8, UTF-16LE and UTF-16BE text file types.
Usage : java com.bruce.main.LogCutter [ -f <config-file> ]
(default config file is '/Users/Kingfisher/MyWork/Java/LogCutter/classes/../conf/config.xml')
------------------------------------------------------------
> !q
be about to shutdown, please wait ...
shutdown perfectly !
- 后台运行演示(Mac 平台):
[Kingfisher@Bruce-mbp LogCutter] $ ./run.sh -d
[Kingfisher@Bruce-mbp LogCutter] $
LogCutter is running in background, use 'kill 4544' to stop me.
[Kingfisher@Bruce-mbp LogCutter] $ kill 4544
[Kingfisher@Bruce-mbp LogCutter] $
!! LogCutter received terminate signal !!
be about to shutdown, please wait ...
shutdown perfectly !
[Kingfisher@Bruce-mbp LogCutter] $
- 程序运行日志
05:52:20,705 INFO [main]: ->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
05:52:20,706 INFO [main]: starting: class com.bruce.main.LogCutter ...
05:52:20,707 INFO [main]: configuration summary
------------------------------------------------------------
[global]
start-check-delay : 1 hours
check-interval : 24 hours
log4j-config-file : /Users/Kingfisher/MyWork/Java/LogCutter/classes/../conf/log4j.properties
lock-file : /Users/Kingfisher/MyWork/Java/LogCutter/classes/../LogCutter.lock
[delete-files] (expire: 30 days)
1. /opt/LogCutter/logs/LogCutter.log*
2. /opt/tomcat6-logs/*.log
3. /opt/tomcat7-logs/*.log
[cut-files] (threshold: 30960 KBs, reserve: 1024 KBs)
1. /opt/apache-logs/*_log
2. /opt/nginx-logs/*.log
3. /opt/tomcat6-logs/catalina.out
4. /opt/tomcat7-logs/catalina.out
5. /var/lib/mysql/*.err
------------------------------------------------------------
05:52:39,062 INFO [pool-1-thread-2]: - - - - - - - -> start CutFileRunner@724646150 <- - - - - - - -
05:52:39,062 INFO [pool-1-thread-1]: - - - - - - - -> start DelFileRunner@334936591 <- - - - - - - -
05:52:39,063 INFO [pool-1-thread-2]: - - - - - - - -> end CutFileRunner@724646150 <- - - - - - - -
05:52:39,063 INFO [pool-1-thread-1]: - - - - - - - -> end DelFileRunner@334936591 <- - - - - - - -
05:52:54,222 INFO [main]: stoping: class com.bruce.main.LogCutter ...
05:52:54,222 INFO [main]: <-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-
- 程序配置文件(./conf/config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<global>
<!-- 开始日志检查延时 (小时), 默认: 0 (立刻开始) -->
<start-check-delay>1</start-check-delay>
<!-- 日志检查间隔 (小时), 默认: 72 -->
<check-interval>24</check-interval>
<!-- Log4J 配置文件, 默认: ${CLASS_ROOT}/../conf/log4j.properties -->
<log4j-config-file></log4j-config-file>
<!-- 程序锁, 默认: ${CLASS_ROOT}/../${APP_NAME}.lock -->
<lock-file></lock-file>
</global>
<!-- 要删除的日志文件列表 -->
<!-- delete-files.expire: 日志文件过期时间(天), 默认: 90 -->
<delete-files expire="30">
<!-- files: 文件名称(不包含目录), 必须填写, 可包含通配符 -->
<!-- files.path: 文件所在目录, 必须填写, 不能包含通配符 -->
<file path="/opt/LogCutter/logs">LogCutter.log*</file>
<file path="/opt/tomcat6-logs">*.log</file>
<file path="/opt/tomcat7-logs">*.log</file>
<!--
<file path="/var/lib/mysql">mysql-bin.*</file>
<file path="/var/lib/mysql">efly-test-relay-bin.*</file>
-->
</delete-files>
<!-- 要截断的日志文件列表 -->
<!-- cut-files.threshold: 日志文件截断阀值(KB), 默认: 10240 -->
<!-- cut-files.reserve: 日志文件保留内容(KB), 默认: 1024 -->
<!--
<!注!> 'cut-files.reserve' 是保留内容的近似值得, 实际内容按行取整保留
如下列日志文件内容:
...... ...... ......
line1: xxxxxxxxxxxxxxxxxxxxx
line2: xxxxxxxxxxPyyyyyyyyyy
line3: zzzzzzzzzzzzzzzzzzzzz
line4: zzzzzzzzzzzzzzzzzzzzz
...... ...... ...... (EOF)
'P'为定位得到的保留起点, 程序实际会在'P'点开始查找下一个换行符,
从该换行符的后一个字符开始保留, 也就是从第三行开始保留到文件末尾
-->
<cut-files threshold="30960" reserve="1024">
<file path="/opt/apache-logs">*_log</file>
<file path="/opt/nginx-logs">*.log</file>
<file path="/opt/tomcat6-logs">catalina.out</file>
<file path="/opt/tomcat7-logs">catalina.out</file>
<file path="/var/lib/mysql">*.err</file>
</cut-files>
</CONFIG>