[A] system log notes - rich Log4j2 Configuration

log4j we should be too familiar, log4j2 is a relatively large upgrade of log4j, the underlying use disruptor rewrite optimized asynchronous log output, performance is greatly improved. I have to log4j, logback, log4j2 done performance comparison, log4j2 indeed showed absolute advantage. Ado, here it is the main author of the project posted continuous improvement log4j2 profile.

  • increased reliance pom.xml
        <!-- 日志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>
    <!--先定义所有的appender -->
    <Appenders>

        <!-- 优先级从高到低分别是 OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL -->
        <!-- 单词解释: Match:匹配 DENY:拒绝 Mismatch:不匹配 ACCEPT:接受 -->
        <!-- DENY,日志将立即被抛弃不再经过其他过滤器; NEUTRAL,有序列表里的下个过滤器过接着处理日志; ACCEPT,日志会被立即处理,不再经过剩余过滤器。 -->
        <!--输出日志的格式
         %d{yyyy-MM-dd HH:mm:ss, SSS} : 日志生产时间
         %p : 日志输出格式
         %c : logger的名称
         %m : 日志内容,即 logger.info("message")
         %n : 换行符
         %C : Java类名
         %L : 日志输出所在行数
         %M : 日志输出所在方法名
         hostName : 本地机器名
         hostAddress : 本地ip地址 -->

        <!-- 控制台 -->
        <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文件 -->
        <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>
                <!--如果是error级别拒绝 onMismatch="NEUTRAL",有序列表里的下个过滤器过接着处理日志; -->
                <ThresholdFilter level="error" onMatch="DENY"
                                 onMismatch="NEUTRAL"/>
                <!--如果是info\warn输出 onMismatch="DENY",日志将立即被抛弃不再经过其他过滤器 -->
                <ThresholdFilter level="info" onMatch="ACCEPT"
                                 onMismatch="DENY"/>
            </Filters>
            <Policies>
                <!-- 如果启用此配置,则日志会按文件名生成新压缩文件, 即如果filePattern配置的日期格式为 %d{yyyy-MM-dd HH}
                    ,则每小时生成一个压缩文件, 如果filePattern配置的日期格式为 %d{yyyy-MM-dd} ,则天生成一个压缩文件 -->
                <TimeBasedTriggeringPolicy/>
                <!-- 每个日志文件最大512MB -->
                <SizeBasedTriggeringPolicy
                        size="512MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*/lucas_device-*.log.gz"/>
                    <!-- 删除30天前的文件 -->
                    <IfLastModified age="30d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <!-- lucas_device_error.log文件 -->
        <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文件 -->
        <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文件 -->
        <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"/>
            <AppenderRef ref="service_appender_error"/>
        </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"/>
        </AsyncLogger>
    </Loggers>
</Configuration>

The main configuration file is an asynchronous system log output to a different file.
lucas_device_error.log exception log file records system;
lucas_device_restful.log recorded with a custom annotations @Logc logging all restful request response;
lucas_device_mqtt.log MqttMessagePublishSolver log record is output class;
last logs are output to the other lucas_device.log file.
It can be seen inside Loggers, we specify the log output destination file, and all logs, including the implementation of sql that we have set, including printed in the Console console.
[Two] system log notes - roll up its sleeves to write a custom log notes

Reproduced in: https: //my.oschina.net/u/872813/blog/3055586

Guess you like

Origin blog.csdn.net/weixin_34194702/article/details/92021112