對于服務器的日常維護來說,日志清理是非常重要的事情,如果殘留日志過多則嚴重浪費磁盤空間同時影響服務的性能。如果用手工方式進行清理,會花費太多時間,并且很多時候難以滿足實際要求。例如:如何在每個星期六凌晨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
--------------------------------------------------
#!/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 !
[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>
下載試用
原文出處:怪獸的博客 怪獸的微博 怪獸樂園Q群