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 -Dlogging.config=/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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值