log4j Configuration Xiang Solution

Source: Internet
Author: User
Tags log log print format

The first step: Join Log4j-1.2.8.jar to Lib.
Step two: Establish log4j.properties under the classpath. The contents are as follows:

1 Log4j.rootcategory=info, stdout, R

2

3 Log4j.appender.stdout=org.apache.log4j.consoleappender

4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

5 LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%c.%m (%l) | %m%n

6

7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender

8 Log4j.appender.r.file=d:\tomcat 5.5\logs\qc.log

9 Log4j.appender.r.layout=org.apache.log4j.patternlayout

Ten Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

11

Log4j.logger.com.neusoft=debug

Log4j.logger.com.opensymphony.oscache=error

Log4j.logger.net.sf.navigator=error

Log4j.logger.org.apache.commons=error

Log4j.logger.org.apache.struts=warn

Log4j.logger.org.displaytag=error

Log4j.logger.org.springframework=debug

Log4j.logger.com.ibatis.db=warn

Log4j.logger.org.apache.velocity=fatal

21st

Log4j.logger.com.canoo.webtest=warn

23

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn

Log4j.logger.org.hibernate=debug

Log4j.logger.org.logicalcobwebs=warn

The third step: the corresponding modification of the properties, before you have to know what these are, in the second part of the explanation.

Fourth step: Add the relevant statement in the class to output the log:

Definition attribute: Protected final Log log = Logfactory.getlog (GetClass ());

In the appropriate method:

if (log.isdebugenabled ())

{

Log.debug ("System .....");

}

Ii. Description of Log4j

1 Log4j.rootcategory=info, stdout, R

This sentence is the output of the log information of level info to stdout and R, the definition of stdout and r in the following code, can be arbitrarily named. Level can be divided into off, FATAL, error, WARN, INFO, DEBUG, all, if the configuration off does not play any information, if configured as info so only display info, WARN, error log information, and DEBUG information will not be displayed, Refer to the third section to define the logger in the configuration file.

3 Log4j.appender.stdout=org.apache.log4j.consoleappender

This sentence defines what type of output is named stdout, which can be

Org.apache.log4j.ConsoleAppender (console),

Org.apache.log4j.FileAppender (file),

Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),

Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)

Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

Refer to the third section to define the Appender in the configuration file.

4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

This sentence is the type of layout that defines the output named StdOut, which can be

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),

Org.apache.log4j.PatternLayout (flexibility to specify layout mode),

Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Refer to the third section to define layout in the configuration file.

5 log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n

If you use the pattern layout to specify the exact format of the printed information Conversionpattern, print the parameters as follows:

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds that the log information is consumed from the application boot to output

%c output belongs to the class, which is usually the full name of the class

%t output The name of the thread that generated the log event

%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"

%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.

[QC] is the beginning of the log information, can be any character, generally referred to as the project abbreviation.

The output information

[TS] DEBUG [main] Abstractbeanfactory.getbean (189) | Returning cached instance of singleton Bean ' myautoproxy '

Refer to the third section to define the formatting log information in the configuration file.

7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender

This sentence is the same as the 3rd line. The type that defines the output that is named R produces a log file every day.

8 Log4j.appender.r.file=d:\tomcat 5.5\logs\qc.log

This is a file named D:\Tomcat that defines the output named R 5.5\logs\qc.log

You can modify it yourself.

9 Log4j.appender.r.layout=org.apache.log4j.patternlayout

Same as Line 4th.

Ten Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

Same as Line 5th.

Log4j.logger.com. Neusoft =debug

Specifies that all classes under the Com.neusoft package are rated as Debug.

You can change the Com.neusoft to the package name that you use for your project.

Log4j.logger.com.opensymphony.oscache=error

Log4j.logger.net.sf.navigator=error

These two sentences are the error level of the two packages, and if Ehcache is not configured in the project, these two sentences are not required.

Log4j.logger.org.apache.commons=error

Log4j.logger.org.apache.struts=warn

These two sentences are struts's packages.

Log4j.logger.org.displaytag=error

This sentence is Displaytag's bag. (used by the QC Problem List page)

Log4j.logger.org.springframework=debug

This sentence is spring's package.

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn

Log4j.logger.org.hibernate=debug

These two sentences are Hibernate's package.

The above package settings can be customized according to the actual situation of the project.

Three, log4j detailed

1. Define the configuration file

LOG4J supports two configuration file formats, one in XML format and one Java attribute file Log4j.properties (key = value). The following describes how to use the Log4j.properties file as a configuration file:

①, configuring the root Logger

Logger is responsible for most of the operations that log records are processed.

Its syntax is:

Log4j.rootlogger = [level], Appendername, Appendername, ...

Level is the priority of logging, which is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or custom levels. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, only if it is equal to or above that level, the log information for all debug levels in the application will not be printed. All: Print all logs, off: Turns off all log output. Appendername is where you specify where the log information is exported. Multiple output destinations can be specified at the same time.

②, configuration log information output destination Appender

The Appender is responsible for controlling the output of logging operations.

Its syntax is:

Log4j.appender.appenderName = Fully.qualified.name.of.appender.class

Log4j.appender.appenderName.option1 = value1

...

Log4j.appender.appenderName.optionN = Valuen

Here the appendername is defined in the ①, can be arbitrarily named.

Among them, LOG4J provides the following types of Appender:

Org.apache.log4j.ConsoleAppender (console),

Org.apache.log4j.FileAppender (file),

Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),

Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size), the file size can be set by LOG4J.APPENDER.R.MAXFILESIZE=100KB, and Log4j.appender.r.maxbackupindex=1 is set to save a backup file.

Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

Example: Log4j.appender.stdout=org.apache.log4j.consoleappender

Define an output destination named stdout, Consoleappender as the console.

③, configuration log Information Format (layout) layout

Layout is responsible for formatting the output of the Appender.

Its syntax is:

Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class

Log4j.appender.appenderName.layout.option1 = value1

...

Log4j.appender.appenderName.layout.optionN = Valuen

Among them, log4j offers the following types of layout:

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),

Org.apache.log4j.PatternLayout (flexibility to specify layout mode),

Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

2. Format log Information

log4j formats the log information in a print format similar to the printf function in C, with the following printing parameters:

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds that the log information is consumed from the application boot to output

%c output belongs to the class, which is usually the full name of the class

%t output The name of the thread that generated the log event

%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"

%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.

3. Use log4j in code

We do the following three jobs in a class that requires output log information:

1, import all the required commongs-logging class:

Import Org.apache.commons.logging.Log;

Import Org.apache.commons.logging.LogFactory;

2. Define a private static class member of the Org.apache.commons.logging.Log class in its own class:

Private final Log log = Logfactory.getlog (GetClass ());

The parameters of the Logfactory.getlog () method use the class of the current classes.

3. Use the member method of the Org.apache.commons.logging.Log class to output log information:

if (log.isdebugenabled ())
{
Log.debug ("111");
}
if (log.isinfoenabled ())
{
Log.info ("222");
}
if (log.iswarnenabled ())
{
Log.warn ("333");
}
if (log.iserrorenabled ())
{
Log.error ("444");
}
if (log.isfatalenabled ())
{
Log.fatal ("555")
}

log4j Configuration Xiang Solution

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.