logback.xml dynamic configurator path

Keywords: xml Spring Zookeeper Java

Catalog

1. Ordinary java program

2.spring boot application

1. Ordinary java program

logback.xml configured as follows

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

    <property name="LOGS_HOME" value="/opt/program/app/logback-demo/logs"/>
    <!-- This value is passed in dynamically when the program is started -->
    <property name="SUB_LOG_HOME" value="" />
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} - %msg%n"/>
    <property name="LOG_CHARSET" value="UTF-8"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
            <charset>${LOG_CHARSET}</charset>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo.log</file>
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
            <charset>${LOG_CHARSET}</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>

    <appender name="ERRORFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo.error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo-%d{yyyy-MM-dd}.error.log</fileNamePattern>
            <maxHistory>20</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="ERRORFILE"/>
    </root>

</configuration>

The property tag consists of name and value, and the name defined above can be referenced by ${name} below.

The value can be written in xml directly, or passed in through the jvm virtual machine parameter configuration when the program is started, which is - dsub ﹣ log ﹣ home = XXX.

Configuration in idea:

2.spring boot application

In the spring boot application, place logback-spring.xml in the resources directory, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://ch.qos.logback/xml/ns/logback" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback http://ch.qos.logback/xml/ns/logback/logback.xsd"
               debug="true" scan="false" scanPeriod="30 seconds">

    <springProperty scope="context" name="logPath" source="logback.logPath"/>
    <springProperty scope="context" name="subLogPath" source="logback.subLogPath"/>

    <!--spring-boot-1.5.10.RELEASE.jar!/org/springframework/boot/logging/logback/defaults.xml-->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

    <property name="LOG_HOME" value="${logPath}"/>
    <!-- This value is passed in dynamically when the program is started -->
    <property name="SUB_LOG_HOME" value="${subLogPath}" />
    <property name="LOG_NAME" value="data-middleware-server-realtime"/>
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39} %line){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} %line - %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.log-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
            <maxHistory>20</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
    </appender>
    <appender name="ERRORFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.error.log-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
            <maxHistory>20</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="REALTIME_IMPORT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${SUB_LOG_HOME}/import/${LOG_NAME}.log</file>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/import/${LOG_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
    </appender>

    <logger name="REALTIME_IMPORT_LOGGER" level="INFO" additivity="false">
        <appender-ref ref="REALTIME_IMPORT_APPENDER"/>
    </logger>

    <logger name="org.apache.zookeeper.ZooKeeper" level="WARN" additivity="true"></logger>
    <logger name="org.apache.zookeeper.ClientCnxn" level="WARN" additivity="true"></logger>

    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="ERRORFILE"/>
    </root>
</configuration>

In addition to using < property > in the configuration file, the < springproperty > variable in which source is the reference spring configuration is also used. In this case, the incoming values of logback.logPath and logback.subLogPath can be passed in through the spring boot configuration file, or they can be passed in dynamically during process startup, such as -- logback.subLogPath=xxx

 

Posted by coco777 on Wed, 20 Nov 2019 11:41:50 -0800