转至某个csdn博客 提供了一个压缩包里面有详细的操作流程。我按流程操作了证明有效果。
百度云资源 :https://pan.baidu.com/s/1BDVfRF8NOa-_3WXiBgz2Yw
提取码: qf54
wind
Tomcat下使用Log4j接管生成日志文件
主要内容:Tomcat下使用Log4j接管生成日志文件,按天存放,解决catalina.out日志文件过大问题。
具体步骤:
官方英文参考路径:http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j
以下是中文描述:
-
准备jar包:
log4j-1.2.17.jar (从 http://www.apache.org/dist/logging/log4j/1.2.17/ 下载)
tomcat-juli.jar, tomcat-juli-adapters.jar (从 http://www.apache.org/dist/tomcat/tomcat-7/v7.0.52/bin/extras/ 下载,根据你的Tomcat版本选择对应的分支)
-
将上面的三个jar包拷贝到 Tomcat 的 lib 目录下;
-
将 tomcat-juli.jar 拷贝到 Tomcat 的 bin 目录下,替换原有的jar包;
-
修改 Tomcat 的 conf/context.xml 文件,将为
(增加 swallowOutput=“true” 的属性配置,只有这样才能完全的把tomcat的stdout给接管过来。这一步很关键 在官网及网上找了许多资料都没有提及。);
-
删除 Tomcat 的 conf/logging.properties 文件(或者重命名-建议);
-
在 Tomcat 的 lib 目录下创建 log4j.properties 文件:
log4j.rootLogger = INFO, CATALINA
Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern={“time”:"%d{yyyy-MM-dd HH:mm:ss,SSS}",“logtype”:"%p",“loginfo”:"%c:%m"}%n
log4j.appender.CATALINA.MaxFileSize=2MB
log4j.appender.CATALINA.MaxBackupIndex=10
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
liunx
Tomcat 利用log4j,日志输出格式为json
进入到Tomcat的lib目录:
cd /usr/local/tomcat8.5.14/lib
wget
http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.42/bin/extras/tomcat-juli-adapters.jar
wget
https://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.jar
进入到Tomcat的bin目录:
cd /usr/local/tomcat8.5.14/bin
mv tomcat-juli.jar tomcat-juli.jar.bak
wget
http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.42/bin/extras/tomcat-juli.jar
进入到Tomcat的conf目录:
mv logging.properties logging.properties.bak
编辑 context.xml,将Context标题修改为如下内容:
进入到tomcat的lib目录:
创建log4j.properties,其catalina日志为滚动日志,json格式输出
vim log4j.properties
log4j.rootLogger = INFO, CATALINA
Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern={“time”:"%d{yyyy-MM-dd HH:mm:ss,SSS}",“logtype”:"%p",“loginfo”:"%c:%m"}%n
log4j.appender.CATALINA.MaxFileSize=2MB
log4j.appender.CATALINA.MaxBackupIndex=10
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
重启Tomcat,并检查日志输出格式:
tail -f usr/log/tomcat8.5.14/logs/catalina
日志输出到catalina,而非catalina.out文件。