Overriding logback configurations

I don’t think that you can overwrite logback.xml-definitions from an included file.

But I have an approach that solves your question regarding overriding the root-logger-level, using variable substitution with default values:

logback.xml

<configuration>
  <include file="includedFile.xml" />

  <!-- STDOUT appender stuff -->

  <root level="${root.level:-DEBUG}">
    <appender-ref ref="STDOUT" />
  </root>
<configuration>

includedFile.xml

<included>

  <!-- override the default value; or comment out to leave it at default -->
  <property name="root.level" value="INFO" />

</included>

With that concept, I’ve even been able to control the output to multiple appenders from the included file:

logback.xml

<configuration>
  <include file="includedFile.xml" />

  <!-- STDOUT appender stuff -->

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>${file.level:-ALL}</level>
    </filter>
    <file>/path/to/logfeil.log</file>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>${syslog.level:-OFF}</level>
    </filter>
    <syslogHost>localhost</syslogHost>
    <facility>${syslog.facility:-LOCAL1}</facility>
    <suffixPattern>${syslog.pattern:-[%thread] %logger{36} - %msg}</suffixPattern>
   </appender>

  <logger name="my.package" level="${logging.level:-INFO}" additivity="false">
    <appender-ref ref="FILE" />
    <appender-ref ref="SYSLOG" />
  </logger>

  <root level="${root.level:-DEBUG}">
    <appender-ref ref="STDOUT" />
  </root>
<configuration>

And in the includedFile.xml I can control if, and at what level message shall pass through the appenders FILE and SYSLOG, setting the properties file.level, syslog.level and of course logging.level.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)