Understanding of Log4J's rootLogger

1 rootLogger
  always has a rootLogger, even if no configuration is displayed, and the default output level is DEBUG
  . All other Loggers inherit from rootLogger by default.
2 The custom Logger (sub Loggger) inherits from rootLogger. The
  format is as follows:
 
quote
log4j.logger.A.B.C

  After this definition, three logger instances are actually created, which are "A", "AB", and "ABC". Every time we get a logger in the system, it is not a new instance. These instances are initialized according to the configuration file when the system starts (or may be created when the first reference is made, and then the instance is cached for later use. I haven't had time to study this part yet).

Call:
  Logger logger = Logger.getLogger("AB")
3 Limit the superposition of appenders
Example 1:
quote
log4j.rootLogger=DEBUG, Console
log4j.logger.A=DEBUG, Console
log4j.logger.A.B=INFO, Console

Any log output by logger AB will be output to the console three times. The reason is that AB inherits all appenders of A and A's parent logger.
This inheritance relationship is only to add the appender of the parent logger to its own appender list. The parent logger The output level does not affect the output of the
child logger.

Example 2: Limiting appender overlays
quote
log4j.rootLogger=DEBUG, Console
log4j.logger.A=DEBUG, Console
log4j.logger.A.B=INFO, Console
log4j.additivity.A.B=false

The logs of logger AB will only be output to its own Console, and will not inherit any parent logger's appender.

The complete configuration is as follows:

log4j.rootLogger=DEBUG,Console,RollingFile

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n

#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=../logs/mylogger.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

#log4j.logger.java.sql=DEBUG
#custom Xconsole
log4j.logger.sd.liveker = ERROR, Xconsole
#Do not inherit the definition of the parent class
log4j.additivity.sd.liveker=false

#Xconsole
log4j.appender.Xconsole=org.apache.log4j.ConsoleAppender
log4j.appender.Xconsole.layout=org.apache.log4j.PatternLayout
log4j.appender.Xconsole.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n





4 Control the output level of the appender
If you want to limit the log level output to the appender, you need to use the threshold to control.
log4j.threshold=ERROR is used to control all appenders, that is, the logs output to all appenders,
regardless of the original level, cannot be lower than the level specified by the threshold.

log4j.appender.Console.threshold=ERROR is used to control the output level of the specified appender.

Reference:
http://www.cnblogs.com/Fskjb/archive/2011/01/29/1947592.html

Guess you like

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