Using log4j to output logs for Tomcat

Source: Internet
Author: User
Tags iso 8601 iso 8601 format system log tomcat log4j

Using log4j to output logs for Tomcat


http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j

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.

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.

The log of Tomcat 5.0. x
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:

Httpservletrequest.getsession (). Getservletcontext (). log ("Some message");

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

The log of Tomcat 5.5. x
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:

Httpservletrequest.getsession (). Getservletcontext (). log ("Some message");

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:

#
# Configures log4j as the Tomcat system logger
# to output info level messages into a rolling log file.
#
Log4j.rootlogger=info, R
#
to continue using the ' catalina.out ' file (which grows forever), # comment out of the above line and
uncomment th E Next.
#
#log4j. Rootlogger=error, A1
#
# Configuration for standard output ("Catalina.out").
#
Log4j.appender.a1=org.apache.log4j.consoleappender
log4j.appender.a1.layout= Org.apache.log4j.PatternLayout
#
Print the date in ISO 8601 format
#
log4j.appender.a1.layout.conversionpattern=%d [%t]%-5p%c-%m%n
#
# Configuration for a rolling log file ("Tomcat.log").
#
Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
log4j.appender.r.datepattern= '. Yyyy-mm-dd
#
# Edit The next line to your logs directory.
# The last part of the name is the log file name.
#
Log4j.appender.r.file=/usr/local/tomcat/logs/tomcat.log
log4j.appender.r.layout= Org.apache.log4j.PatternLayout
#
Print the date in ISO 8601 format
#
log4j.appender.r.layout.conversionpattern=%d [%t]%-5p%c-%m%n
#
# application logging Options
#
#log4j. Logger.org.apache=debug
#log4j. Logger.org.apache=info
#log4j. Logger.org.apache.struts=debug
#log4j. Logger.org.apache.struts=info

You only need to modify this line:

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

Modify the path to your

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.