對於服務器的日常維護來說,日志清理是非常重要的事情,如果殘留日志過多則嚴重浪費磁盤空間同時影響服務的性能。如果用手工方式進行清理,會花費太多時間,並且很多時候難以滿足實際要求。例如:如何在每個星期六凌晨3點把超過2G大的日志文件進行切割,保留最新的100M日志記錄?
網上沒有發覺能滿足本座要求的日志切割工具,因此花了一些閒暇時間自己寫了一個。由於要在多個平台上使用,為了方便采用 Java 實現。本工具命名為 LogCutter,主要有以下特點:
使用方法:
- *******************************************************
- **** LogCutter - Bruce Liang, all rights reserved. ****
- *******************************************************
- 一、啟動方式
- --------------------------------------------------
- ) 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%
- ) Linux / Unix
- A) 前台運行: $ ./run.sh
- B) 後台運行: $ ./run.sh -d
- *** 注 ***
- @ 可以設置為開機時自動後台運行,方法如下:
- $ vi /etc/rc.d/rc.local
- ( 加入: ${YOUR_LOGCUTTER_PATH}/run.sh -d )
- --------------------------------------------------
- 二、配置文件
- --------------------------------------------------
- ) 程序配置文件: ./conf/config.XML
- ) 日志配置文件: ./conf/log4j.propertIEs (默認)
- --------------------------------------------------
- 三、環境要求
- --------------------------------------------------
- ) Java 版 本: JRE 1.6 以上
- ) 依賴程序包: 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)
- ------------------------------------------------------------
- . DelFileRunner@334936591 [ Idle ]
- . 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)
- . /opt/LogCutter/logs/LogCutter.log*
- . /opt/tomcat6-logs/*.log
- . /opt/tomcat7-logs/*.log
- [cut-files] (threshold: 30960 KBs, reserve: 1024 KBs)
- . /opt/apache-logs/*_log
- . /opt/nginx-logs/*.log
- . /opt/tomcat6-logs/catalina.out
- . /opt/tomcat7-logs/catalina.out
- . /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] $
程序運行日志
- :52:20,705 INFO [main]: ->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
- :52:20,706 INFO [main]: starting: class com.bruce.main.LogCutter ...
- :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)
- . /opt/LogCutter/logs/LogCutter.log*
- . /opt/tomcat6-logs/*.log
- . /opt/tomcat7-logs/*.log
- [cut-files] (threshold: 30960 KBs, reserve: 1024 KBs)
- . /opt/apache-logs/*_log
- . /opt/nginx-logs/*.log
- . /opt/tomcat6-logs/catalina.out
- . /opt/tomcat7-logs/catalina.out
- . /var/lib/MySQL/*.err
- ------------------------------------------------------------
- :52:39,062 INFO [pool-1-thread-2]: - - - - - - - -> start CutFileRunner@724646150 <- - - - - - - -
- :52:39,062 INFO [pool-1-thread-1]: - - - - - - - -> start DelFileRunner@334936591 <- - - - - - - -
- :52:39,063 INFO [pool-1-thread-2]: - - - - - - - -> end CutFileRunner@724646150 <- - - - - - - -
- :52:39,063 INFO [pool-1-thread-1]: - - - - - - - -> end DelFileRunner@334936591 <- - - - - - - -
- :52:54,222 INFO [main]: stoping: class com.bruce.main.LogCutter ...
- :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>
下載試用:http://files.cnblogs.com/ldcsaa/LogCutter.zip