logback log file location dynamic specification

Keywords: Java Docker jvm xml

Reference resources: https://stackoverflow.com/questions/19518843/logback-configuration-via-jvm-argument

logback related configuration, where ${log ﹣ home} is the environment variable. When java starts the application, it can set its address dynamically by specifying java - dlog ﹣ home = "path".

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>
     <property name="logPath" value="${LOG_HOME-/home}/logs" />

    <!--  Format of log printing -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>

    <!-- info Log in state   -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %10logger [%thread]  :  %msg%n
            </pattern>
        </encoder>
        <!--Rolling strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <fileNamePattern>${logPath}/info.%d.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
    </appender>

    <!--  Error level log file  -->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %10logger [%thread]  :  %msg%n
            </pattern>
        </encoder>
        <!--Rolling strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
         <fileNamePattern>${logPath}/error.%d.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />
    </root>

</configuration>

In the above code, ${log ﹣ home - / home} / logs means that if you do not pass in the environment variable log ﹣ home, then use the following / home path. For windows systems, / home means the drive letter where the current jar package is located.

Project initiation

 java -Dlogback_home="c:\\logs" -jar eureka.jar

In this way, the log files are stored in the logs directory of disk C.

Specified when docker container

docker run -d -p 7070:7070 -m 200M -e JAVA_OPTIONS='-Dlogback_home="/deployments/'  eurekaserver

Posted by fizzwizz on Tue, 19 Nov 2019 13:27:35 -0800