白季飞龙的个人主页

Log4j大杂烩

Log4j使用SpringBoot风格的日志

1. Spring Boot 引入 log4j

方法一: 引入spring-boot-starter-log4j2,排除spring-boot-starter-logging

pom.xml片段

<dependencies>
    <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>
</dependencies>

方法2: 引入log4j相关依赖,排除spring-boot-starter-logging

pom.xml片段

<dependencies>
    <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.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.11.0</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>1.7.25</version>
    </dependency>

</dependencies>

注意:引入log4j-core:2.11.0会报异常java.lang.NoClassDefFoundError: org/apache/logging/log4j/core/impl/MutableLogEvent

log4j-slf4j-impl:2.11.0依赖于log4j-core:2.10.0,因此引入log4j-slf4j-impl就够了。

引入jul-to-slf4j是为了把java.util.logging的日志输出到log4j。否则java.util.logging的日志将按照老格式输出

像这样:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2018-04-24 13:29:21.774  INFO 7852 --- [           main] b.d.FooApp                               : Starting FooApp on win7-PC with PID 7852 (C:\Users\win7\temp\springone\target\classes started by win7 in C:\Users\win7\temp\springone)
2018-04-24 13:29:21.776  INFO 7852 --- [           main] b.d.FooApp                               : No active profile set, falling back to default profiles: default
2018-04-24 13:29:21.806  INFO 7852 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3fc2959f: startup date [Tue Apr 24 13:29:21 CST 2018]; root of context hierarchy
2018-04-24 13:29:22.196  INFO 7852 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
四月 24, 2018 1:29:22 下午 bj.demo.FooApp onApplicationEvent
信息: JAVA_UTIL_LOGGING
2018-04-24 13:29:22.205  INFO 7852 --- [           main] b.d.FooApp                               : Started FooApp in 0.746 seconds (JVM running for 1.443)
2018-04-24 13:29:22.209  INFO 7852 --- [       Thread-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3fc2959f: startup date [Tue Apr 24 13:29:21 CST 2018]; root of context hierarchy
2018-04-24 13:29:22,224 Thread-4 WARN Unable to register Log4j shutdown hook because JVM is shutting down. Using SimpleLogger

2. 使用SpringBoot风格的日志

引入log4j,但没有配置log4j(比如将log4j2.xml放到资源根目录)的情况下,SpringBoot会使用自带的log4j配置(彩色输出)。

但如果自己配置log4j,需要对输出格式配置颜色,否则不会有彩色输出

log4j2.xml 配置为 spring-boot 格式的彩色输出

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                    pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

漫漫路,莫论逍遥;潜心修,只为悟道