tomcat 分割catalina日志【log4j配置方式】

工具/原料

  • log4j-1.2.16.jar
  • tomcat-juli-adapters.jar
  • tomcat-juli.jar
    *下载地址:http://www.apache.org/dist/tomcat在对应版本中的bin/extras路径下

修改相关配置文件

  1. 修改${catalina.home}/conf/context.xml
    修改:为只有这样才能完全的把tomcat的stdout给接管过来。
  2. 建立log4j.properties
log4j.rootLogger=INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=/applog/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=/applog/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=/applog/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=/applog/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]=\
  1. 复制替换${catalina.home}/bin下的tomcat-juli.jar
  2. 复制log4j-1.2.16.jar、tomcat-juli-adapters.jar和log4j.properties 到tomcat的lib下
  3. 修改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&\
  4. 修改conf/server.xml中localhost_access_log日志路径
    prefix="localhost_access_log." suffix=".txt"
    pattern="%h %l %u %t "%r" %s %b" />
  5. 删除conf/logging.properties文件
  6. 启动tomcat。

你可能感兴趣的:(tomcat 分割catalina日志【log4j配置方式】)