Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Log4j to output catalina.out logs in Tomcat

Shulou Source: shulou.com Published: 2022-06-01 23:03:24 10月14日 Update

How to use Log4j to output catalina.out logs in Tomcat? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Create a log4j.properties file under $CATALINA_BASE/lib

The log4j.properties is as follows:

Log4j.rootLogger = INFO CATALINA# Define all the appenderslog4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.CATALINA.File = ${catalina.base} / logs/catalina.outlog4j.appender.CATALINA.Append = truelog4j.appender.CATALINA.Encoding = UTF-8# Roll-over the log once per daylog4j.appender.CATALINA.DatePattern ='. 'yyyy-MM-ddlog4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout#log4j.appender.CATALINA.layout.ConversionPattern =% d [% t]%-5p% c -% m % nlog4j.appender.CATALINA.layout.ConversionPattern =% d {yyyy-MM-dd HH:mm:ss.SSS}% p [% t]% c |% m%n# configure customed log to catalina.outlog4j.logger.com.xxxxx = WARN CATALINAlog4j.logger.org.apache = WARN, CATALINAlog4j.logger.org.mybatis = WARN, CATALINAlog4j.logger.java.sql = WARN, CATALINAlog4j.logger.org.springframework = WARN, CATALINA

Update tomcat related jar packages

Download log4j-1.2.17.jar (http://www.apache.org/dist/logging/log4j/1.2.17/)

Download 2 jar packages of tomcat7: tomcat-juli.jar and tomcat-juli-adapters.jar (http://www.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/extras/ preferably corresponds to the tomcat version)

Put log4j-1.2.17.jar and tomcat-juli-adapters.jar under $CATALINA_HOME/lib; replace the $CATALINA_HOME/bin/tomcat-juli.jar package with the newly downloaded tomcat-juli.jar package.

Delete $CATALINA_BASE/conf/logging.properties.

Restart tomcat

About the default catalina log format

If you only want to change the default log format of tomcat, replace the default java.util.logging.SimpleFormatter. The format format in the SimpleFormatter class is LoggingSupport.getSimpleFormat (), and its specific value is: "% 1$ tb% 1$ td,% 1$ tY% 1$ tl:%1$ tM:%1$ tS% 1$ Tp% 2$ s%n%4$ s:% 5$ s% 6$ s% n", where the time format may not be what we expected, and there is a newline% n in it. For example, if you want to change the time format similar to the log4j above, you can rewrite a replacement class (com.xxx.LogFormatter). The format is set to "1$ tY-%1$ tm-%1$ td% 1$ tH:%1$ tM:%1$ tS.%1$ tL% 4$ s% 2$ s% 5$ s% 6$ s% n". Modify $CATALINA_BASE/conf/logging.properties

Java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Replace it with the following:

Java.util.logging.ConsoleHandler.formatter = com.xxx.LogFormatterorg.apache.juli.FileHandler.formatter = com.xxx.LogFormatter after reading the above, do you know how to use Log4j to output catalina.out logs in Tomcat? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Format log content output method more problem helpless for this reason skill file time best version article experience industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Redmi macOS OPPO Reno