搜索
您的当前位置:首页正文

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>

因篇幅问题不能全部显示,请点此查看更多更全内容

Top