Spring Boot 使用Log4j2

原创
2017/07/29 14:33
阅读数 1K

前言

Spring Boot 默认使用Logback,来打印日志,这里还想说的SLFJ(Simple Logging Facade for Java),它们之间的关系,一张图,说明一切:

输入图片说明

###maven 配置

<!--use log4j2 property-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <!--flume log4j appender-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-flume-ng</artifactId>
            <version>2.6</version>
        </dependency>

###log4j2.xml 配置 Log4j2 提供非常丰富的Appender,比如CassandraAppender,AsyncAppender,我们这里配置了简单的Flume Appnder,Failover Appender.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
    <Properties>
        <Property name="source">api-web</Property>
        <Property name="log-pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{userId}] [%t] [%-5level] %c{1.}@%M%L - %m%n</Property>
        <!--get product dynamic property-->
    </Properties>
    <Appenders>

        <RollingFile name="RollingFile" fileName="logs/app.log"
                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>${log-pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingFile>

        <Flume name="eventLogger" compress="false" type="avro" ignoreExceptions="false">
            <Agent host="192.168.31.115" port="4444"/>
            <Agent host="127.0.0.1" port="4444"/>
            <PatternLayout>
                <pattern>
                    <pattern>${log-pattern}</pattern>
                </pattern>
            </PatternLayout>
        </Flume>

        <Console name="Console-Appender" target="SYSTEM_OUT" ignoreExceptions="false">
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
        </Console>
        
       <!-- 主要Apeender失败的时候使用备用Appender-->
        <Failover name="Failover" primary="eventLogger">
            <Failovers>
                <AppenderRef ref="RollingFile"/>
            </Failovers>
        </Failover>
    </Appenders>
    <Loggers>
        <!--在需要同时打印文件和发送至flume的时候使用-->
        <!--<Logger name="" level="info" additivity="false">-->
            <!--<AppenderRef ref="RollingFile"/>-->
        <!--</Logger>-->
        <Root level="info">
            <AppenderRef ref="Console-Appender"/>
            <AppenderRef ref="Failover"/>
        </Root>
    </Loggers>
</Configuration>

###总结 如果遇到识别不了log4j2.properties/xml,可以在application.properties,中指定 logging.config=classpath:log4j2.properties/xml

参考

https://springframework.guru/introducing-log4j-enterprise-class-logging/
http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

展开阅读全文
加载中

作者的其它热门文章

打赏
1
51 收藏
分享
打赏
2 评论
51 收藏
1
分享
返回顶部
顶部