Tomcat Log Settings

Source: Internet
Author: User

1 Tomcat log Overview

Tomcat logs are classified into two types:

First, the running logs mainly record the running information, especially the exception and error logs.
The second is access log information, which records access time, IP address, access resource, and other related information.

2 Tomcat log Configuration
2.1 access log Configuration

By default, Tomcat does not record access logs. The following method enables tomcat to record access logs:

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

Comment the following (<! -->.
<! --
<Valve classname = "org. Apache. Catalina. Valves. accesslogvalve"
Directory = "logs" prefix = "localhost_access_log." suffix = ". txt"
Pattern = "common" resolvehosts = "false"/>
-->
2.2 configure tomcat to write more detailed logs

You can modify the pattern in the 2.1 example to change the log output content.

The value can be: Common and combined. The log output content corresponding to the two preset formats is as follows:

Common Value: % H % L % u % T % R % S % B
Combined value: % H % L % u % T % R % S % B % {Referer} I % {User-Agent} I

Pattern can also be freely grouped as needed, such as pattern = "% H % L"

For the meanings of fields, see:

Access log valve entry in http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html
3. Modify the Tomcat running Log Level
3.1 log types and levels

Tomcat logs are classified into the following five categories:

Catalina, localhost, Manager, admin, host-Manager

Logs of each type are classified into the following seven types:

Severe (highest value)> warning> info> config> fine> finer> finest (lowest value)
3.2 how to set the Log Level

Modify the content in CONF/logging. properties to set the log level of a certain type.

Example:

Set the Catalina log level to fine

1catalina.org. Apache. Juli. filehandler. Level = fine

Disable output of Catalina logs:

1catalina.org. Apache. Juli. filehandler. Level = off

Output all log messages of Catalina:

1catalina.org. Apache. Juli. filehandler. Level = all
4. Use log4j to record applicationsProgramLogs or system logs
4.1 Use log4j to output detailed system log information for quick diagnosis of disconnection and startup failures

This example can solve the problem of insufficient error information when Tomcat starts an exception. Use commons-logging and log4j to output detailed log information.

Take tomcat5.5.27 in the window environment as an example:

1. Tomcat decompression directory:

E:/tomcat5.5

2. Set environment variables:

Catalina_home = E:/tomcat5.5

3. Download log4j and commons-logging

Log4j:

Http://logging.apache.org/log4j/1.2/download.html

Commons-logging:

Http://apache.freelamp.com/commons/logging/binaries/commons-logging-1.1.1-bin.zip

4. In this example, place the commons-logging-1.1.1.jar and log4j-1.2.15.jar in the % tomcat_home %/bin directory (which can be placed as needed)

5. Create two file commons-logging.properties, log4j. properties under the % atat_home %/bin directory

The commons-logging.properties file content is as follows:

Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. log4jlogger

The content of the log4j. properties file is as follows:

Log4j. rootlogger = warn, stdout, file

 

# Directly output logs to the console ###

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. stdout. Target = system. Out

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

Log4j. appender. stdout. layout. conversionpattern = % d {absolute} % L-% m % N

 

# Output logs to the file systemout. Log ###

Log4j. appender. File = org. Apache. log4j. fileappender

Log4j. appender. file. File = E:/tomcat5.5/logs/systemout. Log

Log4j. appender. file. append = false

Log4j. appender. file. layout = org. Apache. log4j. patternlayout

Log4j. appender. file. layout. conversionpattern = % d {absolute} % L-% m % N

 

For details about the configuration file, see:

Http://www.minaret.biz/tips/tomcatLogging.html#log4j_properties

 

6. Modify the Catalina. BAT file

Set

Set classpath = % classpath %; % catalina_home %/bin/Bootstrap. Jar

Replace

Set classpath = % classpath %; % catalina_home %/bin/Bootstrap. jar; % catalina_home %/bin/commons-logging-1.1.jar; % catalina_home %/bin/log4j-1.2.13.jar; % catalina_home %/bin

7. log4j is used to output the startup log after startup. bat is enabled.

8. Check the output log in the E:/tomcat5.5/logs/systemout. log file.
4.2 Use log4j in applications

1. Download log4j from the following URL:

Http://logging.apache.org/log4j/1.2/download.html

2. Create a Java project.

3. Add log4j. jar to the compilation path of the project.

4. Create a file named log4j. properties and write the following content:

### Direct log messages to stdout ###

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. stdout. Target = system. Out

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

Log4j. appender. stdout. layout. conversionpattern = % d {absolute} % 5 P % c {1}: % L-% m % N

Log4j. rootlogger = debug, stdout

5. Create a class and add the following content:

Import org. Apache. log4j. Logger;

 

Public class logclass {

Private Static org. Apache. log4j. Logger log = Logger

. Getlogger (logclass. Class );

 

Public static void main (string [] ARGs ){

Log. Trace ("trace ");

Log. debug ("debug ");

Log. Info ("info ");

Log. Warn ("Warn ");

Log. Error ("error ");

Log. Fatal ("Fatal ");

}

}

6. Compile and run the program. You can see the following content in the console:

10:38:24, 797 debug logclass: 11-Debug

10:38:24, 812 info logclass: 12-Info

10:38:24, 812 warn logclass: 13-warn

10:38:24, 812 error logclass: 14-Error

10:38:24, 812 fatal logclass: 15-fatal

7. Output content based on level control logs:

Change log4j. rootlogger = debug and stdout to log4j. rootlogger = warn, stdout

The output content is as follows:

10:41:15, 488 warn logclass: 13-warn

10:41:15, 504 error logclass: 14-Error

10:41:15, 504 fatal logclass: 15-fatal
4.3 configure log4j. properties to change log output content

1. log4j. rootcategory = info, stdout, R

This statement is used to output the log information at the info level to the stdout and R destinations. Levels include off, fatal, error, warn, info, debug, and all. If off is configured, no information is displayed. If Info is configured, only info, warn, but the debug information is not displayed.

2. log4j. appender. stdout = org. Apache. log4j. leleappender

Which type of output end is specified for stdout?

Org. Apache. log4j. leleappender (console ),

Org. Apache. log4j. fileappender (file ),

Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day ),

Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size)

Org. Apache. log4j. writerappender (send log information in stream format to any specified place)

3. log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

This sentence defines the layout type of the output end named stdout.

Org. Apache. log4j. htmllayout (in the form of HTML tables ),

Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),

Org. Apache. log4j. simplelayout (including the log information level and information string ),

Org. Apache. log4j. ttcclayout (including the log production time, thread, category, and other information)

4. log4j. appender. stdout. layout. conversionpattern = [QC] % P [% T] % C. % m (% L) | % m % N

If you use the pattern layout, you must specify the specific format of the print information conversionpattern. The print parameters are as follows:

For specific settings, refer:

Http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

Ø % m: the message specified in the sending code

Ø % P indicates the priority level, that is, debug, info, warn, error, and fatal.

Ø % R indicates the number of milliseconds in which the log information is consumed from startup to output.

Enter the class category in % C, which is usually the full name of the class.

Ø % T line name of the log event generated

Ø % N indicates a carriage return line break. For Windows, the value is "RN", and for UNIX, the value is "N"

Ø date or time at which % d logs are exported. The default format is iso8601. You can also specify the format after this, for example, % d {yyyy mm dd hh: mm: SS, SSS}, input class: October 18, 2002 22: 10: 28,921

Ø % L the location where the log event is generated, including the category name, line generated, and number of lines in the code generation.

[QC] is the beginning of log information. It can be any character. Generally, it is a short term.
5. Related Resources of log4j

The following url provides the introduction of log4j:

Http://logging.apache.org/log4j/1.2/manual.html

Use example of log4j provided at the following URL:

Http://www.laliluna.de/log4j-tutorial.html

From: http://blog.csdn.net/lk_cool/article/details/4561306

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.