tomcat 日志catalina.out 按天自动分割 设定时任务定时清除

时间:2021-12-01 01:18:56

使用log4j分割catalina.out文件(详情可阅读http://tomcat.apache.org/tomcat-7.0-doc/logging.html

一、日志分割所需jar包

1.下载tomcat   apache-tomcat-7.0.79.tar.gz

地址:http://www.apache.org/dist/tomcat/tomcat-7/

2、 压缩包中有三个jar包:

    log4j-1.2.16.jar 

    tomcat-juli-adapters.jar   下载地址:http://www.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/extras/

    tomcat-juli.jar               下载地址:http://www.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/extras/

   说明:如果不是tomcat7版本,请去官网下载对应版本的tomcat-juli.jar,tomcat-juli-adapters.jar类包,

         下载地址: http://www.apache.org/dist/tomcat   在对应版本中的bin/extras 路径下

 

二、修改相关配置文件

1、 修改${catalina.home}/conf/context.xml

修改:<Context >为<Context  swallowOutput="true" >只有这样才能完全的把tomcat的stdout给接管过来。

2、 复制log4j-1.2.16.jar和tomcat-juli-adapters.jar 到tomcat的lib下

3、 复制tomcat-juli.jar到${catalina.home}/bin下,在该目录会存在该文件,覆盖即可

4、 建立log4j.properties,并把其放到到tomcat的/lib下

Log4j.properties的内容如下:相关参数根据需要可以相应修改

log4j.rootLogger=INFO, CATALINA

# Define all the appenders

log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender

#修改tomcat  日志存放路径

log4j.appender.CATALINA.File=/home/richmail/logs/tomcat/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

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

#修改tomcat  日志存放路径

log4j.appender.LOCALHOST.File=/home/richmail/logs/tomcat/localhost.log

log4j.appender.LOCALHOST.Append=true

log4j.appender.LOCALHOST.Encoding=UTF-8

log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd

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

#修改tomcat  日志存放路径

log4j.appender.MANAGER.File=/home/richmail/logs/tomcat/manager.log

log4j.appender.MANAGER.Append=true

log4j.appender.MANAGER.Encoding=UTF-8

log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd

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

#修改tomcat  日志存放路径

log4j.appender.HOST-MANAGER.File=/home/richmail/logs/tomcat/host-manager.log

log4j.appender.HOST-MANAGER.Append=true

log4j.appender.HOST-MANAGER.Encoding=UTF-8

log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd

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

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

 

5. 修改bin/catalina.sh

  a. 屏蔽或删除如下内容:

       (1) if [ -z "$CATALINA_OUT" ] ; then

              CATALINA_OUT=$CATALINA_BASE/logs/catalina.out

            fi

       (2) touch "$CATALINA_OUT"

  

       (3) >> "$CATALINA_OUT" 2>&1 "&"  (两处)

  b. 修改org.apache.catalina.startup.Bootstrap "$@" start \   内容 (两处)

     修改为:org.apache.catalina.startup.Bootstrap "$@" start  & \

         

6. 修改conf/server.xml中localhost_access_log日志路径

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/home/richmail/logs/tomcat "

               prefix="localhost_access_log." suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

7. 删除 conf/logging.properties  文件

8. 启动tomcat。   上述操作实现tomcat 日志按天分割 实现日志定时按天清除 1.在tomcat 的bin 编写 deletelog.sh   清除日志的shell 脚本如下: #!/bin/bash cd `dirname $0` d = `date +%Y d5=`date -d '5 day ago ' +%Y-%m-%d` cd  ../logs rm  -rf catalina.out.${d5} rm  -rf localhost_access_log.${d5}.txt rm -rf locatlhost.log.${d5}   2添加定时任务: #注意修改tomcat的所在路径 0 11 * * *  cd   opt/tomcat/bin; sh deletelog.sh   借鉴: apache:http://tomcat.apache.org/tomcat-7.0-doc/logging.html 博客1:http://blog.csdn.net/seelye/article/details/49424495 博客2:http://blog.csdn.net/lipei1220/article/details/32332429 博客3: http://6949872.blog.51cto.com/6939872/1175530