log4j logger,threshold,additivity Details Note

Source: Internet
Author: User
Tags log4j

Log4j use is relatively simple, but there are many things to be aware of, these matters are not clear often there is a log out of the problem, this article lists some common problems, read this article needs some log4j experience.

1.log4j download, this article uses the SLF4J as the interface, log4j as the implementation class, MAVEN configuration

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>

2. Configuration of the root directory

Log4j.rootlogger = Debug, A1, R
This meaning is the level of the log's root log for debug, the a1,r of the specific log. What is a root log? The root log is the parent of all logs, and if you write a log that is not configured in your code, you will find the root log at this time, if the level of output is greater than the root log, it will be output to the root log, which is equivalent to the default log of the root log.

The above and the log define two output sources A1 and R, where A1 and R need to be configured

Configuration of the A1


Log4j.appender.A1 = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.A1.File = D:/abc/test.log
Log4j.appender.A1.Append = True
Log4j.appender.a1.datepattern=yyyy-ww
Log4j.appender.A1.Threshold = info
Log4j.appender.A1.layout = Org.apache.log4j.PatternLayout
Log4j.appender.A1.layout.ConversionPattern =%5p%10d{yyyy-mm-dd HH:mm:ss} {%l}%m%n

For the above configuration does not understand can Google, here to say the threshold configuration, here threshold configuration is info, but Rootlogger is debug.

Code:

Logger Logger = Loggerfactory.getlogger ("A1");
Logger.debug ("D");

Logger.info ("E");

This only outputs the log of info, not the debug log, because Rootlogger defines the level is debug, but the threshold level is info,info>debug so only the log of info is output. What if the levels of Rootlogger and A1 are reversed.

Log4j.rootlogger = info, A1

Log4j.appender.A1.Threshold = Debug


Logger.debug ("D");

Logger.info ("E");

This will only output the log of info, and will not output debug logs, which can be concluded by code logger and threshold both take the middle log level large.

How do you define a log other than Rootlogger?

The Imitation A1 wrote a B, as follows:

log4j.appender.b= Org.apache.log4j.DailyRollingFileAppender
Log4j.additivity.c=false
Log4j.appender.b.file = D:/abc/b.log
Log4j.appender.b.append = True
Log4j.appender.b.datepattern=yyyy-ww
Log4j.appender.b.threshold = INFO
Log4j.appender.b.layout = Org.apache.log4j.PatternLayout
Log4j.appender.b.layout.conversionpattern =%5p%10d{yyyy-mm-dd HH:mm:ss} {%c:%l}%m%n

Does the code use logger logger = Loggerfactory.getlogger ("B") to output the log? The answer is in the negative.

Because only log b is defined but not used in the code, the following configuration is required:

Log4j.logger.c=debug,b

This defines the configuration used by the code, and the question is whether we use C or B in the code.

The answer is that C,log4j.logger.name defines the name that needs to be used in the code, where the name is C, and the log level is Debug,appender's name B, because this defines Appender's name as B so it needs to configure the Appender configuration of B.

Configured so that the code uses logger logger = Loggerfactory.getlogger ("C");


Log c is equivalent to the newly configured log, and his parent log is the Rootlogger we started talking about, namely A1 and R. In the log4j neutron log output, the parent log will also have the corresponding output code:

Logger Logger = Loggerfactory.getlogger ("C");

Logger.debug ("D");

Logger.info ("E");

In the log of C debug log and info log all output, but in A1 log only info log, why? Because A1 's threshold level is info>debug, information for info is only output.

If A1 does not set its own log level, but the log level of the Log4j.rootlogger configuration is higher than the level of C output, as follows

Log4j.rootlogger=error,a1

Log4j.logger.c=debug,b

Logger Logger = Loggerfactory.getlogger ("C");

Logger.debug ("D");

Logger.info ("E");

Rootlogger level error is greater than the level of C debug, but in the A1 log debug and info all output, this piece is relatively difficult to understand, I understand that if the Rootlogger implementation log does not configure the log level, Even if the Rootlogger is configured with log level, the output of the sub-log does not limit all output. This piece needs to be remembered. If the implementation log is configured with threshold, the output is larger than the log that implements the log level.

What if the subclass does not want the parent class to output? Need to configure:

Log4j.additivity.c=false

This will not output the parent class regardless of the log level.

In the log4j Rootlogger is not configured is also possible, will not error, but the configuration Rootlogger is a good habit, if there is no configuration of the code in the log, at least Rootlogger can log.



log4j logger,threshold,additivity Details Note

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.