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.