LOG4J Basic Tutorials

Source: Internet
Author: User
Tags inheritance log4j

There are several concepts in log4j first, the most important two are logger and appender (please refer to the LOG4J manual), in fact, is the inheritance level and output control.

First, there is always a rootlogger in log4j, even if there is no display configuration, and the default output level is Debug.

Other logger are inherited from this rootlogger (if other logger do not define their output level separately).

The level of log4j is the use of '. ' To separate, such as log4j.logger.com.example.test, this is not to say that the log4j.logger behind must be a specific package name or even the class name,

This name can be customized, we can even define a LOG4J.LOGGER.A.B.C, in the class in Com.example.test named A.b logger, such as

Logger Logger = Logger.getlogger ("a.b")

In the example above, we have established 3 logger instances, namely "A", "a.b", "A.B.C". Every time we get a logger in the system, it's not a new instance, these instances are

The system starts by initializing the configuration file (which is also possible when it is first referenced), and then caches its instances for later use, which has not been studied for some time.

Limitation of Appender superposition

Example 1:

Log4j.rootlogger=debug, Console

Log4j.logger.a=debug, Console

Log4j.logger.a.b=info, Console

Any logs that logger a.b output are exported three times to the console because A.B inherits all Appender of a and the parent logger of a,

This inheritance relationship simply adds the parent logger's appender to its Appender list, and the parent logger's output level does not affect

The output of the child logger.

Example 2: Limit Appender Overlay

Log4j.rootlogger=debug, Console

Log4j.logger.a=debug, Console

Log4j.logger.a.b=info, Console

Log4j.additivity.a.b=false

Logger a.b logs are only exported to their console and do not inherit appender from any parent logger.

Controlling the output level of Appender

If you want to limit the output to the log level in Appender, you need to use threshold to control it.

Log4j.threshold=error is used to control all appender, that is, the output to all Appender logs,

No matter what the original level, can not be lower than the level specified by threshold.

Log4j.appender.console.threshold=error is used to control the output level of the specified appender.

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.