[system log note 1] - rich Log4j2 configuration

Log4j should be more familiar to you. Log4j2 is a relatively large upgrade of log4j. The bottom layer uses disruptor rewriting to optimize the asynchronous output log, which greatly improves the performance. The author has made performance comparison among log4j, logback and log4j2, and log4j2 does show absolute advantages. Not much to say, the following is mainly to post the log4j2 configuration file that the author has been constantly improving in the project.

  • pom.xml increases dependency
        <!-- Journal log4j2 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>${com-lmax-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
  • log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off">
    <Properties>
        <Property name="LOG_HOME">logs</Property>
    </Properties>
    <!--Define all first appender -->
    <Appenders>

        <!-- The priorities from high to low are OFF,FATAL,ERROR,WARN,INFO,DEBUG,ALL -->
        <!-- Word explanation: Match: matching DENY: refuse Mismatch: Mismatch ACCEPT: accept -->
        <!-- DENY,The log will be discarded immediately and will not pass through other filters; NEUTRAL,The next filter in the ordered list goes through and processes the log; ACCEPT,The log will be processed immediately and will not pass through the remaining filters. -->
        <!--Format of output log
         %d{yyyy-MM-dd HH:mm:ss, SSS} : Log production time
         %p : Log output format
         %c : logger Name
         %m : Log content, i.e logger.info("message")
         %n : Newline character
         %C : Java Class name
         %L : Lines of log output
         %M : Log output method name
         hostName : Local machine name
         hostAddress : local ip address -->

        <!-- Console -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
        </Console>
        <!-- lucas_device.log file -->
        <RollingFile name="service_appender" fileName="${LOG_HOME}/lucas_device.log"
                     immediateFlush="false" append="true"
                     filePattern="${LOG_HOME}/$${date:yyyy-MM}/lucas_device-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
            <Filters>
                <!--If it is error Level refusal onMismatch="NEUTRAL",The next filter in the ordered list goes through and processes the log; -->
                <ThresholdFilter level="error" onMatch="DENY"
                                 onMismatch="NEUTRAL"/>
                <!--If it is info\warn output onMismatch="DENY",The log will be discarded immediately and will no longer pass through other filters -->
                <ThresholdFilter level="info" onMatch="ACCEPT"
                                 onMismatch="DENY"/>
            </Filters>
            <Policies>
                <!-- If you enable this configuration, the log generates a new compressed file by file name, that is, if filePattern The configured date format is %d{yyyy-MM-dd HH}
                    ,Generate a compressed file every hour if filePattern The configured date format is %d{yyyy-MM-dd} ,It is naturally a compressed file -->
                <TimeBasedTriggeringPolicy/>
                <!-- Maximum 512 per log file MB -->
                <SizeBasedTriggeringPolicy
                        size="512MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*/lucas_device-*.log.gz"/>
                    <!-- Delete files 30 days ago -->
                    <IfLastModified age="30d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <!-- lucas_device_error.log file -->
        <RollingFile name="service_appender_error" fileName="${LOG_HOME}/lucas_device_error.log"
                     immediateFlush="false" append="true"
                     filePattern="${LOG_HOME}/$${date:yyyy-MM}/lucas_device_error-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT"
                                 onMismatch="DENY"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy
                        size="512MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*/lucas_device_error-*.log.gz"/>
                    <IfLastModified age="30d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <!-- lucas_device_mqtt.log file -->
        <RollingFile name="service_appender_mqtt" fileName="${LOG_HOME}/lucas_device_mqtt.log"
                     immediateFlush="false" append="true"
                     filePattern="${LOG_HOME}/$${date:yyyy-MM}/lucas_device_mqtt-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy
                        size="512MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*/lucas_device_mqtt-*.log.gz"/>
                    <IfLastModified age="7d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <!-- lucas_device_restful.log file -->
        <RollingFile name="service_restful" fileName="${LOG_HOME}/lucas_device_restful.log"
                     immediateFlush="false" append="true"
                     filePattern="${LOG_HOME}/$${date:yyyy-MM}/lucas_device_restful-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy
                        size="512MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*/lucas_device_restful-*.log.gz"/>
                    <IfLastModified age="7d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>

    <Loggers>
        <AsyncRoot level="info" includeLocation="true"
                   additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="service_appender"/>
            <AppenderRef ref="service_appender_error"/>
        </AsyncRoot>
        <AsyncLogger name="com.lucas.device.mqtt.message.MqttMessagePublishSolver" level="info" includeLocation="true"
                     additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="service_appender_mqtt"/>
        </AsyncLogger>
        <AsyncLogger name="com.lucas.device.aop.MainLogAspect" level="info" includeLocation="true"
                     additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="service_restful"/>
        </AsyncLogger>
        <AsyncLogger name="com.lucas.device.mapper" level="debug"
                     includeLocation="true" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="service_restful"/>
        </AsyncLogger>
    </Loggers>
</Configuration>

Configuration files output system logs to different files asynchronously.
The Lucas device error.log file records the system exception log;
Lucas? Device? Restful.log records a custom annotation @Logc Logs of all restful requests recorded, as well as sql execution statements;
Lucas? Device? Mqtt.log records the logs output by the MqttMessagePublishSolver class;
Finally, other logs will be output to lucas_device.log file.
As you can see, in Loggers, we specified the target file for log output, and we set all logs to be printed out in the Console console.
[system log note 2] - roll up the sleeve and write a custom log note

Keywords: Programming log4j xml Spring encoding

Added by npereira on Sat, 02 Nov 2019 22:03:08 +0200