`
darrenzhu
  • 浏览: 806918 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

logback配置Level Inheritance

    博客分类:
  • Java
阅读更多
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 name Assigned level Effective level
root DEBUG DEBUG
X none DEBUG
X.Y none DEBUG
X.Y.Z none DEBUG

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 name Assigned level Effective level
root ERROR ERROR
X INFO INFO
X.Y DEBUG DEBUG
X.Y.Z WARN WARN

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

Example 3

Logger name Assigned level Effective level
root DEBUG DEBUG
X INFO INFO
X.Y none INFO
X.Y.Z ERROR ERROR

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 name Assigned level Effective level
root DEBUG DEBUG
X INFO INFO
X.Y none INFO
X.Y.Z none INFO

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 Name Attached Appenders Additivity Flag Output Targets Comment
root A1 not applicable A1 Since the root logger stands at the top of the logger hierarchy, the additivity flag does not apply to it.
x A-x1, A-x2 true A1, A-x1, A-x2 Appenders of "x" and of root.
x.y none true A1, A-x1, A-x2 Appenders of "x" and of root.
x.y.z A-xyz1 true A1, A-x1, A-x2, A-xyz1 Appenders of "x.y.z", "x" and of root.
security A-sec false A-sec No appender accumulation since the additivity flag is set to false. Only appender A-sec will be used.
security.access none true A-sec Only 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.

 

分享到:
评论

相关推荐

    LogBack配置文件

    LogBack配置文件,主要包括LOGBack的配置文件内容

    logback配置详解

    logback 配置详解 logback 是由 log4j 创始人设计的另一个开源日志组件,它当前分为三个模块:logback-core、logback-classic 和 logback-access。logback-classic 是 log4j 的一个改良版本,同时它完整实现了 slf4...

    springboot-logback配置

    springboot-logback日志文件配置

    SpringBoot+tk.Mybatis整合+yml配置+logback配置

    在“SpringBoot+tk.Mybatis整合+yml配置+logback配置”这个主题中,我们将探讨以下几个关键知识点: 1. **SpringBoot整合tk.Mybatis**: tk.Mybatis 是 Mybatis 的一个扩展,提供了很多实用功能,如:动态 SQL、...

    SpringBoot Logback配置,SpringBoot日志配置

    在Spring Boot中,我们通常通过`application.properties`或`application.yml`配置日志级别和输出位置,但更复杂的配置则需要使用Logback的配置文件`logback.xml`。这个文件应放置在`src/main/resources`目录下,...

    Logback配置文件根据 LEVEL级别将日志分类保存到不同文件.docx

    《Logback配置文件根据LEVEL级别将日志分类保存到不同文件》 日志管理是软件开发中的重要一环,它能帮助开发者追踪程序运行状态,定位问题,优化性能。Logback是一个广泛使用的日志框架,它允许我们高效地处理日志...

    logback日志写logstash配置appender参考

    logback日志写logstash配置appender参考

    springboot整合logback配置文件

    springboot整合logback配置文件

    logback配置文件

    android的logback配置文件,放于assest文件内,此外还需其他的配置才能用

    项目中在使用的logback配置文件

    项目中在使用的logback,拿过去直接可以用,带完整的中文说明。

    Logback类库含logback.xml配置文件

    `logback.xml` 配置文件是 Logback 框架的核心部分,用于定制日志行为。它允许你定义日志级别(如 TRACE, DEBUG, INFO, WARN, ERROR, FATAL 和 OFF),指定日志输出目的地(控制台、文件、数据库等),以及配置过滤...

    logback配置文件解析和示例

    该文件包含logback配置文件示例和配置文件内容解析,如果想深入学习的系哦小伙伴可以下载看看,如果只是想实现功能,可以查看我的博客 《整合篇------JAVA项目整合Logback》

    log4j 和 logback配置资源

    Logback的配置同样通过XML文件完成,其配置灵活性更高,例如支持条件表达式和过滤器,可以实现更精细化的日志管理。 在给定的压缩包中,"log4j.doc"和"logback配置.docx"很可能是两份文档,详细介绍了如何配置和...

    logback日志配置

    ### Logback日志配置详解 #### 一、Logback简介 Logback 是一款非常流行的 Java 日志框架,它由 Ceki Gülcü 开发并维护,作为 log4j 的一个优秀替代品出现。Logback 相对于 log4j 有着更好的性能表现,并且拥有...

    logback配置例子

    logback配置例子,日志系统推荐使用SLF4J,其性能要优于log4j

    logback日志配置demo

    本教程将详细介绍如何配置 logback 以实现日志记录,并探讨不同配置方式。 首先,`pom.xml` 文件是 Maven 项目的配置文件,它包含了项目的依赖信息。在 logback 配置中,我们需要在 `pom.xml` 中引入 logback 相关...

    logback-slf4j日志配置文件-下载即可使用

    "logback-slf4j日志配置文件下载即可使用" logback-slf4j是Java领域中一种常用的日志记录解决方案,它通过结合slf4j(Simple Logging Facade for Java)来提供了异步日志输出的功能,能够将日志输出到不同的文件中...

    Logback配置文件

    logback配置文件demo,用于在springboot项目中放到resource 目录下即可 logback配置文件demo,用于在springboot项目中放到resource 目录下即可

Global site tag (gtag.js) - Google Analytics