Tomcat7下使用Log4j接管catalina.out日志文件

Tomcat7下使用Log4j接管catalina.out日志文件
    
摘要
Tomcat7下使用Log4j接管catalina.out日志文件生成方式,按天存放,解决catalina.out日志文件过大问题。
首先停止tomcat服务: /usr/local/tomcat7/bin/shutdown.sh或者killall -9 java

$CARALINA_HOME/lib即(/usr/local/tomcat7/lib)

1. 准备jar包:
log4j-1.2.17.jar
(从http://www.apache.org/dist/logging/log4j/1.2.17/下载)

tomcat-juli.jar, tomcat-juli-adapters.jar
(从 https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.99/bin/extras/下载,根据你的Tomcat版本选择对应的分支)

2. 将log4j-1.2.17.jar和tomcat-juli-adapters.jar 放入$CARALINA_HOME/lib目录下

3. 将tomcat-juli.jar拷贝到Tomcat的bin目录$CARALINA_HOME/bin下,替换原有的jar包;

4. 在Tomcat的lib目录下创建log4j.properties文件并输入如下内容:
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-HH-mm'.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-HH-mm'.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-HH-mm'.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-HH-mm'.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

4、移动删除或重命名tomcat/conf目录下的logging.properties文件

[root@server conf]# pwd
/usr/local/tomcat7/conf
[root@server conf]# mv logging.properties  /home/

5、修改Tomcat的conf/context.xml文件,将<Context>改为<Context swallowOutput="true">
(增加swallowOutput="true"的属性配置,只有这样才能完全的把tomcat的stdout给接管过来。
  这一步很关键,在官网及网上找了许多资料都没有提及。);

 

 6、启动服务,访问工程,产生日志,查看日志。

posted @ 2022-01-11 18:37  花之旭  阅读(293)  评论(0编辑  收藏  举报