Tomcat log clearing (including extended AccessLogValve) and Tomcat access logs

Source: Internet
Author: User

Tomcat log clearing (including extended AccessLogValve) and Tomcat access logs

1, Tomcat access log AccessLogs regular or quantitative Deletion

AEnable tomcat access Logging

Edit the $ {catalina}/conf/server. xml file. Note: $ {catalina} is the tomcat installation directory.

Comment the following (<! -->.

<! -- <ValveclassName = "org. apache. catalina. valves. AccessLogValve"

Directory = "logs" prefix = "localhost_access_log." suffix = ". txt"

Pattern = "common" resolveHosts = "false"/> -->

Enable the access log for the configuration file and explain the corresponding parameters:

 

BExtend the AccessLogValve by override this function: log (),Set regular or quantitative deletion of access logs

Public void emptyFile () {String logPath = System. getProperty ("catalina. base "); // rotatable = false. Only one log file localhost_access_log.log if (this. rotatable = false) {synchronized (this) {System. out. println ("------ log size is ------:" + this. currentLogFile. length (); if (this. currentLogFile. length () & gt; 10*1024) {this. currentLogFile. delete (); open () ;}}else {// log clearing method containing date '// localhost_access_log.2016-12-16.23.37.log // localhost_access_log.2016-12-16.23.39.log if (new File (logPath ). isDirectory () {// obtain the File set File [] logs = new File (logPath) in the folder ). listFiles (); // set the date format set here, which is consistent with the parameters in the configuration file SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd.HH.mm "); // traverse the set for (int I = 0; I <logs. length; I ++) {File log = logs [I]; // obtain the name of the I log and intercept the Date Field in the middle, convert to long type s int start = log. getName (). indexOf (". ") + 1; int end = log. getName (). lastIndexOf (". "); // The time in the obtained log name (2016-12-16.23.37) String dateStr = logPath. substring (start, end); // convert string type (2016-12-16.23.37) to long type long lonInt = 0; try {lonInt = dateFormat. parse (dateStr ). getTime ();} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println ("------ old-log date is ------:" + lonInt); // the System time minus the time difference obtained in the log name is greater than the time set in the configuration file to delete if (System. currentTimeMillis ()-lonInt)/(1000*60)> 10) {log. delete (); open ();}}}}}

 

2Tomcat common logs (catlina. out, localhost) configure the log4j log framework to implement log rollback

Log4j configures tomcat logs and regularly deletes them:

First, download three jar packages.

Tomcat-juli.jar and tomcat-juli-adapters.jar, log4j. jar

Http://www.apache.org/dist/logging/log4j/1.2.17/

Http://www.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/extras/

 

The Three jar packages are placed in the following directory at a time:

Tomcat-juli-adapters.jar and log4j. jar are placed under the ($ CATALINA_BASE/lib) Directory

Tomcat-juli.jar into $ CATALINA_BASE/bin/directory, directly overwrite the original tomcat-juli.jar can be

 

Delete the $ CATALINA_BASE/conf/logging. properties file to prevent java. util. logging from generating zero-length log files.

 

Modify the conf/context. xml file in Tomcat and set <Context> to <Context swallowOutput = "true">.

 

Finally, create a log4j. properties under the tomcat6.0.43/lib directory, add the following content and save it:

(Taking regular cleaning of catalina. out as an example)

**************************************** **************************************

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] %-5 p % 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] %-5 p % 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] %-5 p % 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] %-5 p % c-% m % n

 

Log4j. appender. CONSOLE = org. apache. log4j. leleappender

Log4j. appender. CONSOLE. Encoding = UTF-8

Log4j. appender. CONSOLE. layout = org. apache. log4j. PatternLayout

Log4j. appender. CONSOLE. layout. ConversionPattern = % d [% t] %-5 p % 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

 

Restart tomcat and you will see that java-Djava. util. logging. config. file is changed to java-Dnop-Djava. util. logging. config. file, indicating that the configuration is successful.

 

The effect is as follows:

 

 

Refer:

Http://alvinalexander.com/java/jwarehouse/apache-tomcat-6.0.16/java/org/apache/catalina/valves/AccessLogValve.java.shtml

Http://code.taobao.org/p/tomcat/diff/3/tc8.0.x/java/org/apache/catalina/valves/AccessLogValve.java

Http://www.tuicool.com/articles/ymyiuiu

Http://sanwen8.cn/p/520j7Qi.html

Http://www.cnblogs.com/dimmacro/p/5566480.html

Http://blog.csdn.net/jackljf/article/details/17428335

Shell Analysis of tomcat access log format

Http://www.cnblogs.com/zhwj184/archive/2013/03/28/3027422.html

Java write logs and regularly delete logs

Http://blog.csdn.net/high2011/article/details/50451216

Http://blog.csdn.net/jsky_studio/article/details/41686597? Spm = 5176.1096799.blogcont.4.wnvuiv>

Related Article

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.