tomcat8使用log4j分割日志

 

 

官方文档参考:https://tomcat.apache.org/tomcat-8.0-doc/logging.html

上面链接有说明怎么用

下方为使用方法找到你的tomcat对应版本

下载地址为:https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/extras/

替换掉你本地版本去找下载即可,我的版本是v8.0.53,所以直接改上面版本下载即可

下载tomcat-juli-adapters.jartomcat-juli.jar

tomcat-juli-adapters放到 tomcat目录\lib下

tomcat-juli放到 tomcat目录\bin下 (切记 切记 切记 是bin目录下)

然后下载log4j的包
下载地址:https://mvnrepository.com/artifact/log4j/log4j

我用的版本是:log4j-1.2.17.jar

将log4j-1.2.17.jar拷贝到 tomcat目录\lib下

删除tomcat本身的logging.properties (在tomcat目录\conf 下面)

创建log4j.properties文件,将文件放到tomcat目录\lib下(注意:不是conf下面)

log4j.rootLogger = INFO, CATALINA

# Define all the appenders
#log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA = org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina.out
log4j.appender.CATALINA.Append = true
log4j.appender.RollingFile.ImmediateFlush=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.MaxFileSize=1MB
log4j.appender.CATALINA.MaxBackupIndex=10
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
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = DEBUG, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] = DEBUG, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] = DEBUG, HOST-MANAGER
log4j.logger.org.apache.catalina.core = DEBUG
log4j.logger.org.apache.catalina.session = DEBUG

修改conf下的context.xml 内容:<Context swallowOutput="true">  !!!

修改/bin/catalina.sh中对应位置

tomcat8使用log4j分割日志

注释掉catalina.sh里面的这一行,不要脚本自己生成!!

if [ -z "$CATALINA_OUT" ] ; then
  #CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
  CATALINA_OUT=/dev/null
fi
上一篇:分割catalina.out 每天生成一个文件


下一篇:logrotate 切割Tomcat的catalina.out文件