log4j configuration solution

log4j configuration solution

第一步:加入log4j-1.2.8.jar到lib下。

第二步:在CLASSPATH下建立log4j.properties。内容如下:

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

10 log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

11

12 log4j.logger.com.neusoft=DEBUG

13 log4j.logger.com.opensymphony.oscache=ERROR

14 log4j.logger.net.sf.navigator=ERROR

15 log4j.logger.org.apache.commons=ERROR

16 log4j.logger.org.apache.struts=WARN

17 log4j.logger.org.displaytag=ERROR

18 log4j.logger .org.springframework=DEBUG

19 log4j.logger.com.ibatis.db=WARN

20 log4j.logger.org.apache.velocity=FATAL

21

22 log4j.logger.com.canoo.webtest=WARN

23

24 log4j.logger.org .hibernate.ps.PreparedStatementCache=WARN

25 log4j.logger.org.hibernate=DEBUG

26 log4j.logger.org.logicalcobwebs=WARN

Step 3: Modify the properties accordingly. Before modifying, you must know what these are for. The second part explains.

Step 4: Add relevant statements to the class to output the log:

define the attribute: protected final Log log = LogFactory.getLog(getClass());

In the corresponding method:

if (log.isDebugEnabled())

{

log.debug(“System .....”);

}

Second, Log4j Description

1 log4j.rootCategory=INFO, stdout , R

This sentence is to output the log information with the level of INFO to stdout and The two destinations of R, stdout and R, are defined in the following code and can be named arbitrarily. The levels can be divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL. If OFF is configured, no information will be displayed. If it is configured as INFO, only the log information of INFO, WARN, and ERROR will be displayed, and the DEBUG information will not be displayed. For specific explanation, please refer to the logger in the third part of the definition configuration file.

3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender

This sentence defines the type of output 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 (generates a new file when the file size reaches the specified size)

org.apache.log4j.WriterAppender (Send log information to any specified place in stream format) For

specific explanations, please refer to the appender in the third part of the definition configuration file.

4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

This sentence defines the type of layout of the output named stdout, which can be

org.apache.log4j.HTMLLayout (layout in HTML form),

org .apache.log4j.PatternLayout (can flexibly specify the layout pattern),

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

org.apache.log4j.TTCCLayout (contains log generation time, thread, Category, etc.)

for specific explanations, please refer to the Layout in the third part of the definition configuration file.

5 log4j.appender.stdout.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n

If you use the pattern layout, you need to specify the specific format ConversionPattern of the printing information, print The parameters are as follows:

%m The message specified in the output code

%p The output priority, namely DEBUG, INFO, WARN, ERROR, FATAL

%r The number of milliseconds it takes to output the log information from the application startup to the output

%c Output the category to which it belongs, usually the full name of the class in which it belongs

%t Output the name of the thread that generated the log event

%n Output a carriage return and line feed, "rn" for Windows platforms and "n" for Unix platforms

%d Output The date or time of the log time point. The default format is ISO8601. You can also specify the format after it, for example: %d{yyyy MMM dd HH:mm:ss,SSS}, the output is similar to: October 18, 2002 22:10 : 28,921

%l Output where the log event occurred, including the category name, the thread in which it occurred, and the number of lines in the code.

[QC] is the beginning of the log information, which can be any character, usually the project abbreviation.

Output information

[TS] DEBUG [main] AbstractBeanFactory.getBean(189) | Returning cached instance of singleton bean 'MyAutoProxy' For

details, please refer to the formatted log information in the third part of the definition configuration file.

7 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

This sentence is the same as line 3. Define the type of output named R to produce a log file per day.

8 log4j.appender.R.File=D:\Tomcat 5.5\logs\qc.log

This sentence defines the file name of the output named R, which is named D:\Tomcat 5.5\logs\qc.log and

can be modified by yourself.

9 log4j.appender.R.layout=org.apache.log4j.PatternLayout

Same as line 4.

10 log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

Same as line 5.

12 log4j.logger.com.neusoft=DEBUG

Specifies that all classes under the com.neusoft package have a DEBUG level.

You can change com.neusoft to the package name used by your own project.

13 log4j.logger.com.opensymphony.oscache=ERROR

14 log4j.logger.net.sf.navigator=ERROR

These two sentences are to set the error level of these two packages to ERROR. If EHCache is not configured in the project, These two sentences are not needed.

15 log4j.logger.org.apache.commons=ERROR

16 log4j.logger.org.apache.struts=WARN

These two sentences are packages of struts.

17 log4j.logger.org.displaytag=ERROR

This sentence is the package of displaytag. (used on the QC question list page)

18 log4j.logger.org.springframework=DEBUG

This sentence is a Spring package.

24 log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN

25 log4j.logger.org.hibernate=DEBUG

These two sentences are the hibernate package.

The settings of these packages 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 is the XML format file, the other is the Java property file log4j.properties (key = value). The following will introduce the method of using the log4j.properties file as the configuration file:

1. Configure the root Logger

Logger is responsible for most of the logging operations.

The syntax is:

log4j.rootLogger = [ level ] , appenderName, appenderName, …

where level is the priority of logging, which is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or a custom level. Log4j recommends using only four levels, the priority from high to low is ERROR, WARN, INFO, DEBUG. With the level defined here, you can control the switch to the corresponding level of log information in the application. For example, if the INFO level is defined here, only the level equal to and higher than this level will be processed, then all the log information of DEBUG level in the application will not be printed out. ALL: print all logs, OFF: turn off all log output. appenderName is to specify where the log information is output. Multiple output destinations can be specified at the same time.

2. Configure the log information output destination Appender

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

where appenderName is defined in ①, Can be named arbitrarily.

Among them, the appenders provided by Log4j are as follows:

org.apache.log4j.ConsoleAppender (console),

org.apache.log4j.FileAppender (file),

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

org .apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size), the file size can be set by log4j.appender.R.MaxFileSize=100KB, and can also be set by log4j.appender.R.MaxBackupIndex=1 Save a backup file.

org.apache.log4j.WriterAppender (Send log information in a stream format to any specified place)

For example: log4j.appender.stdout=org.apache.log4j.ConsoleAppender

defines an output destination named stdout, and ConsoleAppender is the console.

, configure the format (layout) of the log information 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

where Log4j provides The layouts are as follows:

org.apache.log4j.HTMLLayout (layout in HTML form),

org.apache.log4j.PatternLayout (layout patterns can be flexibly specified),

org.apache.log4j.SimpleLayout (contain log information level and information string),

org.apache.log4j.TTCCLayout (contains log generation time, thread, category, etc.)

2 , Format log information

Log4J uses a print format similar to the printf function in C language to format log information. The printing parameters are as follows:

%m The message specified in the output code

%p output priority, namely DEBUG, INFO, WARN, ERROR, FATAL

% rOutput the number of milliseconds it takes to output the log information from the application startup

% cOutput the category to which it belongs, usually the full name of the class in which it belongs

% tOutput the thread name that generated the log event

%nOutput a carriage return and linefeed, Windows The platform is "rn", and the Unix platform is "n".

%d The date or time of the log time point is output. The default format is ISO8601. You can also specify the format after it, for example: %d{yyyy MMM dd HH:mm:ss, SSS}, the output is similar: October 18, 2002 22:10:28, 921

%l Output where the log event occurred, including the category name, the thread in which it occurred, and the number of lines in the code.

3. Using Log4j in the code

We do the following three tasks in the class that needs to output log information:

1. Import all the required commons-logging classes:

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 your own class:

private final Log log = LogFactory.getLog(getClass()); The parameters of the

LogFactory.getLog() method use The class of the current class.

3. Use the member methods 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")
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647497&siteId=291194637