Using log4j to output logs for Tomcat

Source: Internet
Author: User
Tags static class system log tomcat apache tomcat log4j

using log4j to output logs for Tomcat

Original source:

Geoff Mottram (Geoff at minaret Dot biz).

Placed in the public domain on August, 2004 by the author.

Last Updated:january 27, 2005.

This document is provided ' as is ', without warranty of any kind, express or implied, including and not limited to the Warr Anties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author is liable for any claim, damages or other liability, whether in an action of contract, Tort O R Otherwise, arising from, out of or in connection with this document or the use or other dealings in this document.

Brief introduction
To be able to meet the special requirements for log output in one of my projects, I spent several hours configuring log4j to output logs for Tomcat5.0.28. This article explains some of the mechanisms and describes the steps to configure Tomcat to generate a log file daily, covering both tomcat5.0.x and 5.5.x versions.

Note: The latest log4j 1.3 Alpha Beta has been supported to generate a log file daily. Refer to the related links in this article to learn how to install and configure.

Tomcat 5.0 . x's Log
Tomcat 5.0.x Records log information primarily from two channels. The standard log output file is Catalina.out and typically contains a date servlet log file in the file name (for example, Catalina.2004-08-27.log).

The data in Catalina.out consists of three sources: data output to standard input and standard error output, log information output via commons logging interface, and a variety of Tomcat event states and other systems such as Jakarta Struts) output. Using the Commons logging interface, you can output a wide variety of log data as configured.

The data in the servlet is generated by calling the log () method of the ServletContext class, for example:

To save the output information to the log file, you need to configure the FileLogger in the Tomcat configuration file (Tomcat's conf directory) Server.xml, typically as follows:  
       <!--Global logger unless overridden at lower levels-->
      <logger Classname= "Org.apache.catalina.logger.FileLogger"
               prefix= "servlet." suffix= ". Log" timestamp= "true"/>

The servlet log can be turned on (configured with FileLogger) and turned off (no configuration FileLogger), but there is no way to specify the log output level (only the info and higher level log information can be output). Although the Tomcat document mentions that all of its logger support verbosity configuration items, it seems to have little effect.

If you want to configure Tomcat to capture all of the log data, output to a log file that generates one daily (for example, the date indicated in the file name). Tomcat in FileLogger provides a standard way to capture the servlet log stream and generate a new file every day. You can back up, delete, analyze, or other actions you want for the log files.

If Tomcat had another Catalina logger using the Commons logging interface, it would give you better control over the format of the output, although the module is fairly easy to implement, but who has the time to do it. We'll show you how to control catalina.out.

Tomcat 5.5 . x's Log
Tomcat 5.5.x has two ways to record log data, write all content of standard output and standard error output, or record in file Catalina.out, and all remaining logs are output via Commons Logging interface. If you are not configured for other logger (such as log4j), all log information (including all content that is output through commons-logging) is output to catalina.out, causing it to grow.

One improvement of Tomcat 5.5.x over Tomcat 5.0.x is that the servlet log is not present, and the servlet log is used to receive any content passed to the log () method of the ServletContext class, for example:

Tomcat 5.5.x now outputs this information through the Commons-logging interface as an info-level log. If you configure a logger like log4j, you can enter all the log information into a log file generated daily (e.g. in the file name to indicate the resulting date), you can back up, delete, analyze the log files or other actions you want.

Tomcat General Log
As mentioned above, most of the data in the Catalina.out file is generated by calling the Commons logging interface, Commons logging allows you to control the log output details at run time, and allows the use of a variety of background log systems. The Tomcat standard release uses Simplelog as the logging system, and simplelog outputs all of the content to the standard error output, for Tomcat, The default is redirected to the Catalina.out file, which causes the Catalina.out file to grow continuously without generating a circular log file.

Log4j
LOG4J is a powerful log system that supports Commons logging, which can be added to your Tomcat to replace Simplelog to support circular log files and diverse configurations. In fact, not only can you output the log to a file, you can output it to the operating system log, or other targets such as remote machines, you can also output to more than one place. To use the log4j system in your tomcat, you can follow these steps to install the configuration (note: This is for the log4j 1.2.x version, the 1.3.x Alpha beta version of the installation and configuration can refer to the relevant link in the appendix): If Tomcat is currently running, Turn Tomcat off. Download the commons Logging release package from the Apache website. Unzip the Commons-logging.jar in the compressed package into the Common/lib directory of your Tomcat installation path. Download the log4j release package from the Apache website. Unzip the Log4j-1.2.12.jar in the compressed package into the Common/lib directory of your Tomcat installation path. The common/classes of the Tomcat installation path creates the Log4j profile log4j.properties (configuration file in the following section). Re-start Tomcat

The configuration described here causes two log files to be created in Tomcat 5.5.x and three files created in Tomat 5.0.x: One is a servlet log file (tomcat 5.0.x only), and a new log file is recycled every night The second is the Commons Logging log file, which is output via log4j (which also generates new log files every night), There is also a catalina.out file that contains only print to standard output and standard error output, this file will continue to grow, but if your tomcat application is well designed, it is not a problem to use standard output and standard error output (in fact, this file should be size 0).

Note that Commons-logging.jar and Log4j.jar files are installed in the Tomcat Common/lib directory, and the Log4j.properties file is installed in the Common/classes directory. This configuration makes it possible for both Tomcat and your Web application to use log4j. If your Web application uses the Commons logging interface, its log data will be output to the Tomcat service log. You can also modify the log4j configuration file log4j.properties to output your application log to other files (see log4j for additional documentation).

log4j configuration file
The Log4j.properties file can be configured as follows:

You only need to modify this line:

Log4j.appender.r.file=/usr/local/tomcat/logs/tomcat.log

Modify the path to your tomcat's logs directory, and if your system environment is configured with the CATALINA_HOME environment variable, you can configure it with ${catalina.home}/logs/tomcat.log.

The above log4j configuration records only the more important logs (warning warning, error errors, Fatal error fatal) to the Tomcat.log file in Tomcat's logs directory, the first message that is over 0 points per night triggers the renaming of Tomcat.log, appends the current date to the Tomcat.log file name, and produces a new tomcat.log.

The bad thing about using log4j Dailyrollingfileappender is that in order to generate a looping log file, you must output a log message to trigger, and if you use cron every night to process your log files, there is a problem. Tomcat's own FileLogger always resolves this issue by adding a date to the log file name of the day. I (Geoff) have written a custom log4j appender to mimic Tomcat's filelogger, which you can use for free (including source code). The separate technical tip has datedfileappender installation and configuration information.

Note: The new log4j 1.3.x Alpha beta version already supports rolling log files by day. Refer to log4j Version 1.3 and Apache tomcat for information on how to install and configure.

You can override the default logging level by following the last few lines of the configuration file to make a special configuration for your application. In a formal operating environment, you may want to minimize log output, and you can modify this line:

Log4j.rootlogger=info, R

For:

Log4j.rootlogger=error, R

Warning
Be careful when modifying the log output level to debug (especially like Log4j.logger.org.apache), which can cause a large number of logs and slow down your system to a considerable degree.

Tomcat Start
Note that you have to install Commons-logging.jar into Tomcat's Common/lib directory and you may have noticed that there is already a Commons-logging-api.jar file in Tomcat. This jar file is a simple version of Commons-logging.jar, only implements the Simplelog and similar other parts, can only basically meet the system startup log output, because no log system api,tomcat can not be started. If the log system (such as log4j) is not included in the startup Classpath, it is not possible to replace the file with Commons-loging.jar.

During the boot process some classes are loaded, if you are in common/ The Lib directory joins the full version of Commons-logging.jar, which replaces the Commons-logging-api.jar class, initializes the log system, and attempts to locate log4j or other log systems that you use.

Application log
You can use the servlet log (such as ServletContext.log ("Some message") in your servlet program to log messages, or take advantage of the Commons logging interface. This section describes the use of better logging methods to demonstrate the enhancement of logs after the integration described above.

Routines:

 
 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 
 

The log object can be declared as a static class member variable and shared among multiple class objects, in other words, your entire program can share a unique log object, or multiple log objects, to control a variety of logging generation methods at run time. You pass the fully qualified name of the class to the Logfactory.getlog () method to uniquely identify the log object, and in the example above, the name of the log should be com.acme.webapp.MyApplication. Give each log object a name that can be used to control the output of the log with the same representation in Log4.properties. In this way, you can add the following line to define the output debug information for your app:

 

You can use any string to name the log object, but, by convention, you name the log object using class classes, and refer to the FAQ for log4j to learn about naming methods.

 

http://minaret.biz/tips/tomcatLogging.html

http://minaret.biz/tips/log4j.html

Http://minaret.biz/tips/datedFileAppender.html

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.