spring boot log4j2使用

一、前言:

spring boot支持的日志框架有,logback,Log4j2,Log4j和Java Util Logging,默认使用的是logback日志框架,这里我决定不用默认的logback,试试使用 log4j2(log4j2是log4j的升级版,因为log4j已经不更新了),本文章主要介绍spring boot集成log4j2日志框架。

二、集成log4j2

pom.xml排除默认logback依赖,加入log4j2

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
    <!--排除默认自带的logback依赖-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

默认的properties配置对log4j2不够友好,我们应用外部配置文件,在资源文件夹src/main/resources下添加log4j2.xml,配置文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%highlight{%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %M() @%L - %msg%n}{FATAL=Bright Red, ERROR=Bright Magenta, WARN=Bright Yellow, INFO=Bright Green, DEBUG=Bright Cyan, TRACE=Bright White}"/>
        </Console>
        <!--<File name="ERROR" fileName="log/error.log" append="false">-->
        <!--<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>-->
        <!--<PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %M() @%L - %msg%n"/>-->
        <!--</File>-->
        <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingInfoFile" fileName="log/app.log"
                     filePattern="logs/info/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %M() @%L - %msg%n"/>
            <SizeBasedTriggeringPolicy size="5MB"/>
        </RollingFile>

        <!-- error 日志 -->
        <RollingFile name="RollingErrorFile" fileName="log/error.log"
                     filePattern="logs/error/$${date:yyyy-MM}/error-%d{MM-dd-yyyy}-%i.log.gz">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %M() @%L - %msg%n"/>
            <SizeBasedTriggeringPolicy size="5MB"/>
        </RollingFile>

    </Appenders>
    <Loggers>
        <Root level="INFO">
            <!--<appender-ref ref="ERROR" />-->
            <appender-ref ref="RollingInfoFile"/>
            <appender-ref ref="RollingErrorFile"/>
            <appender-ref ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

配置完成重启工程,日志输出格式将使用该配置的格式输出
application.properties 中加入配置:

logging.config= src/main/resources/log4j2.xml

使用:

@RestController
@RequestMapping("/index")
public class IndexController {
private static Logger logger= LoggerFactory.getLogger(IndexController.class);

猜你喜欢

转载自blog.csdn.net/fengcai0123/article/details/79374518