LOG4J Configuration Specific explanation

Source: Internet
Author: User
Tags log log print format

These days because the project needs are studying the use of log4j, see an article. Write ultra-specific, do not want to stash, and small friends to share, the text such as the following:

The first step: increase 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 changes in the properties, before the change must know what these are, in the second part of the decomposition said.

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

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

In the corresponding method:

if (log.isdebugenabled ())

{

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

}

Ii. Description of Log4j

1 Log4j.rootcategory=info, stdout, R

This sentence outputs the log information of level info to both the STDOUT and r destinations. StdOut and R are defined in the following code. Can be named at will. Rank can be divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all. Assuming that configuration off does not play any information, assume that the configuration is info so that only the log information of info, WARN, error is displayed, and debug information is not displayed. Detailed explanation can be taken in the third section defines 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 a randomly specified place)

Detailed explanation can be taken in the third section defines the Appender in the configuration file.

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

This sentence defines the type of layout for 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 (including the level of log information and the information string),

Org.apache.log4j.TTCCLayout (includes information such as the time, thread, category, etc.) of the log generation

Detailed explanation The third section defines the layout in the configuration file.

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

Assume that you use the pattern layout to specify the information that you want to print in the detailed format Conversionpattern, print the parameters such as the following:

%M the message specified in the output code

%p the output priority level. That is Debug. Info,warn,error,fatal

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

The class to which the%c output belongs. is usually the full name of your 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 date or time of the output log point in time. The default format is ISO8601. You can also specify a format later. For example:%d{yyyy MMM DD hh:mm:ss,sss}. Output similar to: October 18, 2002 22:10:28. 921

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

[QC] is the beginning of the log information. Can be a random character, usually a project abbreviation.

The output information

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

Detailed explanation The third section defines 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

The name of the file that defines the output named R is D:\Tomcat 5.5\logs\qc.log

Can change it on its own.

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.

Ability to change Com.neusoft to the package name used by your project.

Log4j.logger.com.opensymphony.oscache=error

Log4j.logger.net.sf.navigator=error

These two sentences are to set the error level below the two packages as errors, assuming that there is no configuration Ehcache in the project, then 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.

Iii. specific explanations of log4j

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 your own defined levels. LOG4J recommends using only four levels. Priority levels from high to low are error, WARN, INFO, DEBUG, respectively. By the level defined here. A switch that you can control to the corresponding 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 offers 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 through LOG4J.APPENDER.R.MAXFILESIZE=100KB and can be set to save a backup file via Log4j.appender.r.maxbackupindex=1.

Org.apache.log4j.WriterAppender (send log information in stream format to a randomly specified place)

For 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

Of 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 (including the level of log information and the information string),

Org.apache.log4j.TTCCLayout (includes information such as the time, thread, category, etc.) of the log generation

2. Format log Information

LOG4J formatted the log information in a print format similar to the printf function in C, printing the parameters such as the following:

%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

The class to which the%c output belongs. is usually the full name of your class

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

%n outputs a carriage return newline character, and the 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, for example:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

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

3. Use log4j in code

We do the following three jobs in classes that need to output log information, for example:

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")
}
We can also look at the blogger's other articles, Bo Master finishing ability is pretty strong.


Original address: http://www.blogjava.net/kit-soft/archive/2009/08/28/292977.html
Blog Address: http://www.blogjava.net/kit-soft/

LOG4J Configuration Specific explanation

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.