首页 > 代码库 > tomcat 使用log4j进行日志切割

tomcat 使用log4j进行日志切割

因为tomcat catalina.out日志不会自己主动切割,

一、日志切割所需包在附近中

1、 压缩包中有三个jar包:
    log4j-1.2.16.jar 
    tomcat-juli-adapters.jar
    tomcat-juli.jar

   说明:假设不是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
log4j.appender.CATALINA.File=/home/richmail/logs/tomcat/catalina.log
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
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
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
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 使用log4j进行日志切割