Override Slf4j logback.xml for a Docker Deployed application

Using env variables:

Create two env variable to hold the log directory location and log level setting respectively. Then use that variables in the logback.xml. Then pass the appropriate values while running the docker image

docker run --env LOG_DIR=/opt/logs --env LOG_LEVEL=DEBUG <<DOCKER IMAGE NAME>>

Using logback.configurationFile setting:

We can override the logback.xml file externally. So assume that you have the logback.xml under the src/main/resources and want to override it with an external file.

Assume that its a docker containerized application, so update the dockerfile and pass the logback.configurationFile with the appropriate external file path

ENTRYPOINT java -jar -Dlogback.configurationFile=/opt/config/logback.xml  /opt/rest.jar

VOLUME /opt/config

You can also specify the volumes in docker compose file to mount the appropriate folder if your application is a distributed one. Make sure that the logback.xml file should have the scan attribute otherwise the changes wont be reflected

Example logback.xml file is given below

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/tmp/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/tmp/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>
    </root>
</configuration>
</code>

“scanPeriod” is an optional attribute, by default its 1 minute.

Advertisements

Override Slf4j logback.xml for a java application

The system property [logback.configurationFile] needs to be passed with the proper logback file path to override the default configuration.

  java -Dlogback.configurationFile=/opt/logback.xml -jar /opt/restApp1.0.jar

If you want the logback-classic to automatically refresh the changes, then the scan attribute should be passed as true. otherwise it would not be reloaded. We have to manually restart the server/application. This attribute is available in the root node[configuration]

<configuration scan="true"> 
 ...
 ...
 </configuration> 
By default logback-classic will scan the changes every one minute, but if we want to
 change this then we have to add another attribute scanPeriod with the time interval and the unit as below.
<configuration scan="true" scanPeriod="30 seconds"> 
 ...
 ...
 </configuration>