Tomcat uses Log4j to output catalina.out log

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, which has several shortcomings. The file catalian.out cannot be generated daily like log4j, and will become larger and larger. The log format is inconsistent with that printed by log4j in the project, which is not conducive to parsing.

I searched on the official website of Tomcat (https://tomcat.apache.org/tomcat-7.0-doc/logging.html) and found that by modifying some configurations and replacing the extension package, you can use log4j to output catalian.out.

Create a log4j.properties file in $CATALINA_BASE/lib

The content of log4j.properties is as follows:

log4j.rootLogger = INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina.out
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
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
#log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CATALINA.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] %c | %m%n
# configure customed log to catalina.out
log4j.logger.com.xxxxx = WARN, CATALINA
log4j.logger.org.apache = WARN, CATALINA
log4j.logger.org.mybatis = WARN, CATALINA
log4j.logger.java.sql = WARN, CATALINA
log4j.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 two 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 corresponding to the tomcat version)

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

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, just replace the default java.util.logging.SimpleFormatter . The 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 expect, and there is a line break %n in it. For example, if you want to change to a time format similar to the log4j above, you can rewrite a replacement class (com.xxx.LogFormatter) by yourself and set the format 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

Change it to the following:

java.util.logging.ConsoleHandler.formatter = com.xxx.LogFormatter
org.apache.juli.FileHandler.formatter = com.xxx.LogFormatter

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Detailed explanation of three ways to cut catalina.out logs in tomcat
  • Solve the problem of catalina.out continuing to accumulate in Tomcat
  • Catalina.out log file segmentation under Linux tomcat
  • Tomcat8 uses cronolog to split Catalina.Out logs

<<:  MySQL Oracle and SQL Server paging query example analysis

>>:  How to use physics engine joints in CocosCreator

Recommend

JavaScript implements circular progress bar effect

This article example shares the specific code of ...

Detailed explanation of Nginx http resource request limit (three methods)

Prerequisite: nginx needs to have the ngx_http_li...

What to do if the auto-increment primary key in MySQL is used up

In the interview, you should have experienced the...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

Overview of the definition of HTC components after IE5.0

Before the release of Microsoft IE 5.0, the bigges...

HTML fixed title column, title header table specific implementation code

Copy code The code is as follows: <!DOCTYPE ht...

Introduction to CSS BEM Naming Standard (Recommended)

1 What is BEM Naming Standard Bem is the abbrevia...

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

Detailed steps to install nginx on Apple M1 chip and deploy vue project

brew install nginx Apple Mac uses brew to install...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...