Detailed explanation of three ways to cut catalina.out logs in tomcat

Detailed explanation of three ways to cut catalina.out logs in tomcat

1. Log4j for log segmentation

1) Prepare three packages: log4j-1.2.17.jar tomcat-juli.jar tomcat-juli-adapters.jar and put them in the lib directory of tomcat or WEB_INF/lib of the project.

2) Create a new log4j.properties file in the lib directory and add the following content

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'.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'.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

3) In the tomcat root directory, in the conf folder, delete or rename the logging.properties file. Then modify the context.xml file and change the <Context> tag to <Context swallowOutput="true"> to take over the log output of Tomcat. In this way, log segmentation using log4j is completed.

2. Use cronolog to split logs

Cronolog is a filter program that reads log file entries from standard input and writes each entry to the output file specified by the file name template and the current log file. When the file extension changes, the current file is closed and a new file is opened. Cronolog is designed to be used with web servers such as Apache to split access logs into daily or monthly logs.

# wget https://files.cnblogs.com/files/crazyzero/cronolog-1.6.2.tar.gz
# tar -zxf cronolog-1.6.2.tar.gz
# cd cronolog
# ./configure 
# make && make install
# which cronolog
# /usr/local/sbin/cronolog

Edit bin/catalina.sh under tomcat (Note: the number of lines may not be mine, just find the statement)

Replace the above two contents with:

org.apache.catalina.startup.Bootstrap "$@" start \
2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &

Once completed, restart tomcat.

3. Use logrotate to cut.

In /etc/logrotate.d, create a new tomcatrotate, edit tomatrotate, and write the following content:

/usr/local/tomcat7.0.79/logs/catalina.out {
daily
rotate 15
missingok
dateext
compress
notifempty
copytruncate
}

daily specifies the dump cycle as daily
rotate 15 specifies the number of times the log file is rotated before being deleted. 0 means no backup, and 5 means 5 backups are retained.
missingok If the log does not exist, ignore the warning message
The file suffix of dateext is in date format, that is, the file after cutting is: xxx.log-20150828.gz
compress Compress the dumped logs using gzip (gzip -d xxx.gz to decompress)
notifempty If the file is empty, do not dump it.

copytruncate is used for log files that are still open to back up and truncate the current log.

/usr/local/tomcat7.0.79/logs/catalina.out specifies the path of catalina.out

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Tomcat uses Log4j to output catalina.out log
  • 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

<<:  A record of pitfalls in JS regular matching

>>:  MySQL knowledge points for the second-level computer exam mysql alter command

Recommend

Docker implements container port binding local port

Today, I encountered a small problem that after s...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

About WeChat Mini Program to implement cloud payment

Table of contents 1. Introduction 2. Thought Anal...

React introduces antd-mobile+postcss to build mobile terminal

Install antd-mobile Global import npm install ant...

Summary of the top ten problems of MySQL index failure

Table of contents background 1. The query conditi...

JS realizes special effects of web page navigation bar

This article shares with you a practical web navi...

Web page creation question: Image file path

This article is original by 123WORDPRESS.COM Ligh...

Centos 7 64-bit desktop version installation graphic tutorial

If you think the system is slow and want to chang...

Flex layout allows subitems to maintain their own height

When using Flex layout, you will find that when a...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...