logback配置Level Inheritance

logback中,未指定级别的logger会从其祖先中继承日志级别。默认情况下,root logger的级别为DEBUG。日志输出可以被发送到多个目的地,这些目的地称为appender,可以附加到logger并按层次结构继承。appender的追加性意味着日志请求将被发送到logger及其所有祖先的appender。如果将logger的additivity标志设置为false,则可以阻止这种追加行为。
摘要由CSDN通过智能技术生成
Configuration file example:
<configuration debug="true">
......
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread %property{PROCESS_ID}] %-5level [%logger{10}] - %msg%n</Pattern>
</layout>
<target>System.out</target>
</appender>
 
<appender name="xyz_ERR" class="ch.qos.logback.core.rolling.RollingFileAppender">
......
</appender>
   
 
<logger name="x">
<level value="ERROR" />
</logger>
 
<logger name="x.y">
<level value="DEBUG" />
</logger>
 
<logger name="x.y.z">
<level value="ERROR" />
<appender-ref ref="xyz_ERR" />
</logger>
 
<root>
<level value="DEBUG" />
<appender-ref ref="FULL_ROLLING" />
<appender-ref ref="STDOUT" />
</root>
 
</configuration>

 

 
 
http://logback.qos.ch/manual/architecture.html#effectiveLevel

Effective Level aka Level Inheritance

Loggers may be assigned levels. The set of possible levels (TRACE, DEBUG, INFO, WARN and ERROR) are defined in thech.qos.logback.classic.Level class. Note that in logback, the Level class is final and cannot be sub-classed, as a much more flexible approach exists in the form of Marker objects.

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

The effective level for a given logger L, is equal to the first non-null level in its hierarchy, starting at L itself and proceeding upwards in the hierarchy towards the root logger.

To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level. By default, this level is DEBUG.

Below are four examples with various assigned level values and the resulting effective (inherited) levels according to the level inheritance rule.

Example 1

Logger nameAssigned levelEffective level
rootDEBUGDEBUG
XnoneDEBUG
X.YnoneDEBUG
X.Y.ZnoneDEBUG

In example 1 above, only the root logger is assigned a level. This level value, DEBUG, is inherited by the other loggers XX.Y andX.Y.Z

Example 2

Logger nameAssigned levelEffective level
rootERRORERROR
XINFOINFO
X.YDEBUGDEBUG
X.Y.ZWARNWARN

In example 2 above, all loggers have an assigned level value. Level inheritance does not come into play.

Example 3

Logger nameAssigned levelEffective level
rootDEBUGDEBUG
XINFOINFO
X.YnoneINFO
X.Y.ZERRORERROR

In example 3 above, the loggers rootX and X.Y.Z are assigned the levels DEBUGINFO and ERROR respectively. Logger X.Y inherits its level value from its parent X.

Example 4

Logger nameAssigned levelEffective level
rootDEBUGDEBUG
XINFOINFO
X.YnoneINFO
X.Y.ZnoneINFO

In example 4 above, the loggers root and X and are assigned the levels DEBUG and INFO respectively. The loggers X.Y and X.Y.Zinherit their level value from their nearest parent X, which has an assigned level.

 

http://logback.qos.ch/manual/architecture.html serach "appenders and Layouts"

Appenders and Layouts

The ability to selectively enable or disable logging requests based on their logger is only part of the picture. Logback allows logging requests to print to multiple destinations. In logback speak, an output destination is called an appender. Currently, appenders exist for the console, files, remote socket servers, to MySQL, PostgreSQL, Oracle and other databases, JMS, and remote UNIX Syslog daemons.

More than one appender can be attached to a logger.

The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say L, then enabled logging requests for L and L's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag of a logger to false.

The rules governing appender additivity are summarized below.

Appender Additivity

The output of a log statement of logger L will go to all the appenders in L and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger L, say P, has the additivity flag set to false, then L's output will be directed to all the appenders in L and its ancestors up to and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default.

The table below shows an example:

Logger NameAttached AppendersAdditivity FlagOutput TargetsComment
rootA1not applicableA1Since the root logger stands at the top of the logger hierarchy, the additivity flag does not apply to it.
xA-x1, A-x2trueA1, A-x1, A-x2Appenders of "x" and of root.
x.ynonetrueA1, A-x1, A-x2Appenders of "x" and of root.
x.y.zA-xyz1trueA1, A-x1, A-x2, A-xyz1Appenders of "x.y.z", "x" and of root.
securityA-secfalseA-secNo appender accumulation since the additivity flag is set to false. Only appender A-sec will be used.
security.accessnonetrueA-secOnly appenders of "security" because the additivity flag in "security" is set to false.

More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating a layout with an appender. The layout is responsible for formatting the logging request according to the user's wishes, whereas an appender takes care of sending the formatted output to its destination. The PatternLayout, part of the standard logback distribution, lets the user specify the output format according to conversion patterns similar to the C languageprintf function.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值