36

Is there any way that we can override the logback configurations? I know that we define the logback configurations in file named logback.xml (usually stored in the path src/main/resources) and I know that by using <include> tag we can set an external file to be added to logback.xml just like below:

<configuration>

<!--<include url="file:///d:/ServerConfig.xml"/>-->
<include file="${outPut}/ServerConfig.xml"/>


<logger name="Server" LEVEL="DEBUG">
    <appender-ref ref="FILEOUT" />
</logger>

<root level="DEBUG">
    <appender-ref ref="STDOUT" />
    <!--<appender-ref ref="FILEOUT" />-->
</root>

</configuration>

But what if I want to override this default configuration? For example set the root logger level to INFO.

Here is the included file:

<included>

<!-- <property file="d:/ServerSysVar.properties"/>-->
<property file="${outPut}/ServerSysVar.properties"/>


<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>



<appender name="FILEOUT" class="ch.qos.logback.core.FileAppender">
    <file>${Sys_Location}/Serverfile4.log</file>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %class{36}.%M %L CLIENT_PORT:%X{ClientPort}- %msg%n</pattern>
    </encoder>
</appender>


<logger name="Service" LEVEL="DEBUG">
    <appender-ref ref="FILEOUT" />
</logger>

 <root>
    <appender-ref ref="STDOUT" />
   <!-- <appender-ref ref="FILEOUT" />-->
</root>

</included>
xlm
  • 6,854
  • 14
  • 53
  • 55
Mr.Q
  • 4,316
  • 3
  • 43
  • 40
  • There is a bug reported in 2011 that intends to solve issue by overriding configuration with System Properties - http://jira.qos.ch/browse/LOGBACK-239 – erkfel Mar 17 '16 at 23:05
  • 2
    That discussion shows remarkable stubbornness of the author resisting to change anything for some *academic* nonsense. I wonder how this implementation can be still alive? – tomasb Dec 22 '16 at 17:38

2 Answers2

28

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.

Hank
  • 4,597
  • 5
  • 42
  • 84
5

I had a similar problem to which this answer seems the best approach.

In my case, I needed to override the included file to completely turn off one of the appenders.

Using the a variable for the log level value, one can change it to OFF.

<included>
  (...)
  <root level="${root.level.console:-DEBUG}">
    <appender-ref ref="CONSOLE" />
  </root>
  <root level="${root.level.file:-DEBUG}">
    <appender-ref ref="FILE" />
  </root>
</included>

As stated in http://logback.qos.ch/manual/configuration.html#rootElement

<configuration>
  <include resource="base.xml" />
  (...)
  <property name="root.level.console" value="OFF" />
</configuration>
tiago
  • 195
  • 1
  • 7
  • This variant doesn't work for me. I use logback version 1.1.3. What version did you use? At least it doesn't work for appender's file tag. I tried to override filename from included file – Geniy Mar 17 '16 at 16:46
  • It's important to set __ before __ – Geniy Mar 18 '16 at 16:49
  • The documentation says "at most one element", so if this works it probably isn't intentional on their part. Seems like it'd be better to put the element inside your included file. – Shannon Apr 12 '17 at 23:01