multiple root loggers with logback conditionals

一笑奈何 提交于 2020-01-04 04:05:32

问题


referring to: Logback's Configuration

my configuration can contain ...at most one <root> element...

but then later in the same doc, when discussing conditionals, I see this:

<configuration debug="true">
  <if condition='property("HOSTNAME").contains("torino")'>
    <then>
      <appender name="CON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
          <pattern>%d %-5level %logger{35} - %msg %n</pattern>
        </encoder>
      </appender>
      <root> <------ root logger #1
        <appender-ref ref="CON" />
      </root>
    </then>
  </if>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${randomOutputDir}/conditional.log</file>
    <encoder>
      <pattern>%d %-5level %logger{35} - %msg %n</pattern>
   </encoder>
  </appender>

  <root level="ERROR"> <------ root logger #2
     <appender-ref ref="FILE" />
  </root>
</configuration>

Notice there are TWO <root> elements! I'm very confused because I don't see any <else> elements and I would assume the FILE appender and the second root logger are still in play, even if the hostname was "torino".

How is this a valid example? Why are two <root> loggers allowed in this case when one is not within an <if> or an <else>

what am I missing???


回答1:


Disclaimer: I am the maintainer of the logback project.

This definitely deserves a clarification in the logback documentation. Can you please file a bug report at http://jira.qos.ch ? Referencing this StackOverflow entry should be enough.

As for which root logger is active, assuming the conditional is true, they will be both active. Both appenders named FILE and CON would be attached to the root logger. The level would be set to the last value set. Note that the root element within the conditional does not set a level.



来源:https://stackoverflow.com/questions/41647036/multiple-root-loggers-with-logback-conditionals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!