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">
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s