Tomcat catalina.out log file is too large

Source: Internet
Author: User
Tags log4j

Today found a server/opt directory data is too large, and finally found that the Catalina.out log in tomcat caused by too large

Use the du command to view the data file size for the next layer of opt
[Email protected] opt]# du-h--max-depth=1/opt

Eventually found a large file catalina.out in Tomcat.
[Email protected] logs]# ll-h/opt/liferay6.2ce/tomcat-7.0.42/logs/catalina.out
-rw-r--r--1 root root 12G Oct 16:40 catalina.out

Empty this log file
[Email protected] logs]# cat/dev/null> catalina.out
---------------------

The log file generated by Tomcat by default Catalina.out, increases over time, and may reach G order of magnitude. File is too large, we will not be able to use the regular editing tools to view, seriously affect the system maintenance work. Solve this problem, mainly from Tomcat and code two aspects of consideration.

First, the code itself is considered
Tomcat outputs the Catalina.out file, the main source of data is: System.out and System.err directly on the console output information. In order to reduce the pressure of the log output, standardize code habits, you should avoid using System.out.println () and E.printstacktrace () encoding.

Second, Tomcat configuration considerations

1. Tomcat uses log4j
As we know, Tomcat uses Java.util.logging for log printing by default, and Linux prints the application log on Catalina.out by default. When the system is running for a long time, we will find the problem: the Catalina.out log volume is getting bigger, it is very inconvenient for us to view and manage. (the default Tomcat Catalina log does not support a file that is configured every day)
To solve the above problems there are several ideas, online discussion More is the log cutting, such as through the Cronolog tool for daily cut. This approach is not a cure for the symptoms, not flexible enough. Our Web projects typically use log4j for log printing, and log4j natively supports printing logs by day (per month/by hour/by minute), as well as a rich set of configurations that can be implemented with only a few modifications to the Tomcat configuration.
1) Prepare the jar package:
Log4j-1.2.17.jar (Download)
Tomcat-juli.jar,tomcat-juli-adapters.jar (download, select the corresponding branch based on your Tomcat version)
2) Copy the above three jar packages to the Tomcat Lib directory, and copy the Tomcat-juli.jar to the Tomcat bin directory, replacing the original jar package;
3) Modify the Tomcat conf/context.xml file, change <Context> to <context swallowoutput= "true", and reduce the output of the console log.
(Add swallowoutput= "true" to the property configuration, only in order to fully take the Tomcat stdout to take over.) This step is crucial in the official website and the internet to find a lot of information is not mentioned. );
4) Delete Tomcat's conf/logging.properties file (or rename-suggest);
5) Copy the log4j.properties files in the project to the Lib directory of Tomcat;
6) Restart Tomcat

2. Tomcat Configuration
When you deploy the service, modify the Tomcat conf/context.xml file, change <Context> to <context swallowoutput= "true" >;
3, using the Cronolog tool Division (Cronolog use)
A, Cronolog tool download
Official url:http://cronolog.org/download/index.html
B, Cronolog compile and install
[Email protected] ~]$ tar xvf cronolog-1.6.2.tar.gz
[Email protected] cronolog-1.6.2]$./configure
[[email protected] cronolog-1.6.2]$ make
[[email protected] cronolog-1.6.2]$ sudo make install
C. Modify the Tomcat startup script catalina.sh
A. Modify the output log path
Modify:
If [-Z "$CATALINA _out"]; Then
catalina_out= "$CATALINA _base"/logs/catalina.out
Fi
For:
If [-Z "$CATALINA _out"]; Then
catalina_out= "$CATALINA _base"/logs/catalina.%y-%m-%d.out
Fi
B. Delete the build log file
Comments:
Touch "$CATALINA _out"
For:
#touch "$CATALINA _out"
C. Modify the startup script parameters
Modify:
Org.apache.catalina.startup.Bootstrap "[email protected]" start \
>> "$CATALINA _out" 2>&1 "&"
For:
Org.apache.catalina.startup.Bootstrap "[email protected]" Start 2>&1 \
| /usr/local/sbin/cronolog "$CATALINA _out" >>/dev/null &
D. Restart Tomcat
The Tomcat output log file is split successfully and the output log file format is: catalina.2014-08-15.out type.

Tomcat catalina.out log file is too large

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.