简体   繁体   English

在Spring Boot应用程序中使用log4j2进行日志记录

[英]Logging with log4j2 in Spring Boot app

I am trying to learn Spring Boot. 我正在尝试学习Spring Boot。 But I completely mess up with a loggers dependencies. 但我完全陷入了记录器依赖关系。 I have a simple pom: 我有一个简单的pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

I've created log4j2.xml in ..src\\main\\resources\\ 我在..src \\ main \\ resources \\中创建了log4j2.xml

I logging with something like this: 我用这样的东西记录:

private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
LOG.error("ERRRRRRR!!!!");

But when app starts I see: 但当应用程序启动时,我看到:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.3/log4j-slf4j-impl-2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

And of course log4j2 and its config don't work. 当然log4j2及其配置不起作用。

I know, it is qute popular question. 我知道,这是一个流行的问题。 I googled a lot of answers, but nothing works for me. 我搜索了很多答案,但对我来说没什么用。

You have to exclude the logback that comes bundled with spring-boot-starter-batch, and replace it with spring-boot-starter-log4j2. 您必须排除spring-boot-starter-batch捆绑的logback,并将其替换为spring-boot-starter-log4j2。 Check out the documentation for logging and Spring Boot. 查看有关日志记录和Spring Boot的文档

This pom configuration works well for me. 这种pom配置对我很有用。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
        <version>${spring.boot.version}</version>
        <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>
        <version>${spring.boot.version}</version>
    </dependency>

You have multiple SLF4J bindings in your classpath. 您的类路径中有多个SLF4J绑定。

  • Logback classic, which is included by default in spring boot Logback经典版,默认包含在spring boot中
  • Log4j that you want to use. 你想要使用的Log4j。

You need to have a single one in the classpath, by using maven dependency exclusion. 通过使用maven依赖项排除,您需要在类路径中拥有一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM