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

Detailed explanation of jquery tag selector application example

This article example shares the specific code of ...

JavaScript implements simple scroll window

This article example shares the specific code of ...

Detailed example of using js fetch asynchronous request

Table of contents Understanding Asynchrony fetch(...

Docker builds cluster MongoDB implementation steps

Preface Due to the needs of the company's bus...

Docker nginx + https subdomain configuration detailed tutorial

Today I happened to be helping a friend move his ...

MySQL extracts Json internal fields and dumps them as numbers

Table of contents background Problem Analysis 1. ...

HTML page jump code

Save the following code as the default homepage fi...

Example of how to install nginx to a specified directory

Due to company requirements, two nginx servers in...

How to generate a free certificate using openssl

1: What is openssl? What is its function? What is...

Draw a heart with CSS3

Achieve resultsRequirements/Functionality: How to...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...