tomcat7配置log4j解决catalina.out日志过大问题

tomcat默认安装后,会每天会自动切割日志文件,(catalina.2010-10-08.log、catalina.2010-10-09.log、catalina.2010-10-10.log)这样的形式,但是他的catalina.out不清空,任其自由增长,时间长了系统性能急剧下降。


要解决catalina.out过大问题,网上有很多种方法,如:使用日志切割工具、或者自己写定时脚本:

#!/bin/bash

cd `dirname $0`
d=`date +%Y%m%d`
d7=`date -d'7 day ago' +%Y%m%d`

cd ../logs/

cp catalina.out catalina.out.${d}
cat /dev/null > catalina.out
rm -rf catalina.out.${d7}
对其日志进行切割每天生成一个文件,并且删除7天前的文件。


其次,还可以对tomcat进行log4j配置,使用log4j的日志“切割”方式。配置如下:

1)下载与tomcat对应版本的tomcat-juli.jar 和 tomcat-juli-adapters.jar,或者tomcat-extras-juli-7.0.70.jar和tomcat-extras-juli-adapters-7.0.70.jar:

https://mvnrepository.com/artifact/org.apache.tomcat.extras
https://mvnrepository.com/artifact/org.apache.tomcat


2)将log4j.jar 和 tomcat-juli-adapters.jar放到$CATALINA_HOME/lib目录下;


3)将log4j.properties 放到$CATALINA_HOME/lib目录下;


4)用新下载的tomcat-juli.jar替换掉$CATALINA_HOME/bin/中的tomcat-juli.jar;(下载的tomcat-juli.jar包含了所有Commons Logging实现,能够发现log4j和配置)


5)删除$CATALINA_BASE/conf/logging.properties


6)修改 Tomcat 的 conf/context.xml 文件,将改为

7)重启tomcat


log4j.rootLogger = INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
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
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

参考: http://my.oschina.net/xldc/blog/298080
https://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j


你可能感兴趣的:(java)