一、下載cronolog工具,我下載的版本是cronolog-1.6.2
(yum安裝:yum install cronolog)
二、將下載好的文件解壓,tar xvzf cronolog-1.6.2.tar.gzjava
三、切換到解壓後的文件目錄下:cd cronolog-1.6.2spring
四、初始化和編譯安裝apache
./configure --prefix=/usr/local/cronolog #make #make install #ln -s /usr/local/cronolog/sbin/* /usr/local/sbin/
五、查看安裝版本tomcat
#cronolog --version
六、修改tomcat的啓動文件(tomcat目錄/bin/catalina.sh)服務器
vi catalina.sh
(1)修改輸出日誌路徑
修改:app
if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out fi
爲:工具
if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out fi
(2)刪除生成日誌文件
註釋:spa
touch "$CATALINA_OUT" 爲: #touch "$CATALINA_OUT"
(3)修改啓動腳本參數(兩項)
修改:
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
爲:
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
七、重啓tomcat
tomcat輸出日誌文件分割成功,輸出log文件格式爲:catalina.2015-06-30.out.net
1.首先編輯logrotate.conf文件,打開compress選項(去掉註釋)hibernate
[root@localhost ~]# cat /etc/logrotate.conf | grep -v "^$"| grep -v "#" weekly rotate 4 create dateext compress <<這一項
2.添加指定文件,在/etc/logrotate.d/目錄下新建一個名爲tomcat的文件
[root@localhost ~]# cat >/etc/logrotate.d/tomcat <<EOF /home/tomcat/utr/logs/catalina.out{ #要輪轉的文件 copytruncate # 建立新的catalina.out副本後,截斷源catalina.out文件 daily # 天天進行catalina.out文件的輪轉 rotate 7 # 至多保留7個副本 missingok # 若是要輪轉的文件丟失了,繼續輪轉而不報錯 compress # 使用壓縮的方式(節省硬盤空間;一個2~3GB的日誌文件能夠壓縮成60MB左右) size 16M # 當catalina.out文件大於16MB時,就輪轉 } EOF
3.執行方式
①自動執行原理
1.天天晚上crond守護進程會運行在/etc/cron.daily目錄中的任務列表; 2.與logrotate相關的腳本也在/etc/cron.daily目錄中。運行的方式爲"/usr/bin/logrotate /etc/logrotate.conf"; 3./etc/logrotate.conf文件include了/etc/logrotate.d/目錄下的全部文件。還包括咱們上面剛建立的tomcat文件; 4./etc/logrotate.d/tomcat文件會觸發/usr/local/apache-tomcat-8.0.28/logs/catalina.out文件的輪轉。
②手動執行:logrotate /etc/logrotate.conf
③只輪轉剛剛的tomcat配置文件:logrotate --force /etc/logrotate.d/tomcat
log4j.rootLogger=INFO, CATALINA log4j.logger.org.apache=INFO, CATALINA log4j.logger.org.[hibernate](http://lib.csdn.net/base/javaee)=WARN, CATALINA log4j.logger.org.springframework=WARN, CATALINA # Define all the appenders log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out log4j.appender.CATALINA.Append=true log4j.appender.CATALINA.Encoding=UTF-8 # Roll-over the log once per day log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost. log4j.appender.LOCALHOST.Append=true log4j.appender.LOCALHOST.Encoding=UTF-8 log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender log4j.appender.MANAGER.File=${catalina.base}/logs/manager. log4j.appender.MANAGER.Append=true log4j.appender.MANAGER.Encoding=UTF-8 log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager. log4j.appender.HOST-MANAGER.Append=true log4j.appender.HOST-MANAGER.Encoding=UTF-8 log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Encoding=UTF-8 log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n # Configure which loggers log to which appenders # Configure which loggers log to which appenders log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\ INFO, MANAGER log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\ INFO, HOST-MANAGER