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

Common ways to optimize Docker image size

The Docker images we usually build are usually la...

Where is the project location deployed by IntelliJ IDEA using Tomcat?

After IntelliJ IDEA deploys a Javaweb project usi...

Research on the value of position attribute in CSS (summary)

The CSS position attribute specifies the element&...

How to calculate the frame rate FPS of web animations

Table of contents Standards for smooth animation ...

Navicat imports csv data into mysql

This article shares with you how to use Navicat t...

How to reset the root password in Linux mysql-5.6

1. Check whether the MySQL service is started. If...

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

Quickly solve the Chinese input method problem under Linux

Background: I'm working on asset reporting re...

Detailed explanation of how to use JavaScript paging component

The pagination component is a common component in...

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...

Introduction to the use of base link tag base

<br />When you click the link, the web page ...

Steps to deploy multiple tomcat services using DockerFile on Docker container

1. [admin@JD ~]$ cd opt #Enter opt in the root di...

How to deploy a simple c/c++ program using docker

1. First, create a hello-world.cpp file The progr...