So, my goal was that if I execute my application without any extra Logback configuration, everything goes to console. And in case configuration file exists in the environment, Logback would use that and forget the default configuration.
The default console logging configuration is in the root of classpath and is named as logback.xml (the file Logback looks for by default). Here's the contents the file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<define name="INCLUDED_FILE_EXISTS" class="ch.qos.logback.core.property.FileExistsPropertyDefiner">
<path>${configpath}/included-logback.xml</path>
</define>
<if condition='property("INCLUDED_FILE_EXISTS").equals("true")'>
<then>
<!-- this configuration is used in other than development environments i.e. custom config per environment -->
<include file="${configpath}/included-logback.xml"/>
</then>
<else>
<!-- This configuration is used only in development enviroment -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
<logger name="fi.foo.bar.package" level="DEBUG"/>
</else>
</if>
</configuration>
This configuration has two important sections. First one is the use of FileExistsPropertyDefiner. It checks the existence of the given file in file system and sets value of property INCLUDED_FILE_EXISTS to true or false. If you want, you can also check existence of a file on classpath by using ResourceExistsPropertyDefiner. The variable configpath is defined in system properties. Second interesting section is where the INCLUDED_FILE_EXISTS property is used to include the external file. If the file was found, its configuration is used and default configuration is discarded completely.
Here is example of the included file. The file has name included-logback.xml.
<included scan="true">
<!-- This file is scanned so you can update it while application is running -->
<appender name="FILELOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${configpath}/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${configpath}/logs/application-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILELOG" />
</root>
</included>
Here is example of the included file. The file has name included-logback.xml.
<included scan="true">
<!-- This file is scanned so you can update it while application is running -->
<appender name="FILELOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${configpath}/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${configpath}/logs/application-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILELOG" />
</root>
</included>