日志管理


Log4j2

在 pom.xml 中引入 Log4j2 的 Starter 依赖 spring-boot-starter-log4j2 ,同时排除默认引入的 spring-boot-starter-logging,比如下面这样:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</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>

在配置文件 application.properties 中,通过 logging.config 配置指定 log4j2 的配置文件位置,比如下面这样:

在 resource 目录下新建 log4j2.xml(这里不绝对,根据第二步中配置的内容来创建),然后加入 log4j2 的日志配置,比如,下面这样:


Source & Reference