ythy / blog

Give everything a shot
7 stars 0 forks source link

Log4j Level Inheritance #357

Open ythy opened 4 years ago

ythy commented 4 years ago

reference

If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level.

To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level. Below are four tables with various assigned level values and the resulting inherited levels according to the above rule.

Example 1

Loggername Assignedlevel Inheritedlevel
root Proot Proot
X none Proot
X.Y none Proot
X.Y.Z none Proot

In example 1 above, only the root logger is assigned a level. This level value, Proot, is inherited by the other loggers X, X.Y and X.Y.Z.

Example 2

Loggername Assignedlevel Inheritedlevel
root Proot Proot
X Px Px
X.Y Pxy Pxy
X.Y.Z Pxyz Pxyz

In example 2, all loggers have an assigned level value. There is no need for level inheritence.

Example 3

Loggername Assignedlevel Inheritedlevel
root Proot Proot
X Px Px
X.Y none Px
X.Y.Z Pxyz Pxyz

In example 3, the loggers root, X and X.Y.Z are assigned the levels Proot, Px and Pxyz respectively. The logger X.Y inherits its level value from its parent X.

Example 4

Loggername Assignedlevel Inheritedlevel
root Proot Proot
X Px Px
X.Y none Px
X.Y.Z none Px

In example 4, the loggers root and X and are assigned the levels Proot and Px respectively. The loggers X.Y and X.Y.Z inherits their level value from their nearest parent X having an assigned level..

code

log4j.rootLogger: The root logger resides at the top of the logger hierarchy. com.imc.saml.SSOClient: That`s only a path of package

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS} %5p %c:%L -- %m%n

log4j.logger.com.imc.saml.SSOClient=WARN, A2
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS}  %m%n
ythy commented 4 years ago

If missing rootLogger setting:

log4j.logger.com.imc.saml.SSOClient=WARN, A2
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS}  %m%n

Cause Error:

log4j:WARN No appenders could be found for logger (org.opensaml.core.config.InitializationService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Only see log in com.imc.saml.SSOClient