首页 > 解决方案 > Log4J2 - 从 Eclipse 运行时不发生日志记录

问题描述

Eclipse: 2019-06 (4.12.0)
Java: 1.8.0_201
Log4J2: whatever is pulled in by spring-boot-starter-log4j2 - might be 2.11.2 (mvn dependency:tree)
Spring Boot: 2.1.6.RELEASE

我有一组使用 Log4J2 进行日志记录的 Spring Boot 服务。日志记录设置为两个捕获:1) 控制台输出到文本日志文件和 2) 由特定类生成的输出以将统计信息报告到单独的 JSON 文件。

当我通过启动脚本(在 Linux 上)执行服务时,输出会正确写入它们各自的文件。但是,当我尝试从 eclipse 中运行服务(用于开发测试/调试和错误修复)时,输出总是出现在控制台中。从来没有产生任何文件。我尝试了很多东西,但没有任何改变。

我在控制台中看到的似乎表明,如果有的话,Log4J2没有用于日志记录,而是 Logback. 我已经在 POM 文件中排除了 Logback,但它仍在使用中。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/JO24447/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/JO24447/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.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]

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

2020-03-10 09:06:48.489  INFO 8204 --- [           main] e.m.l.m.s.r.ApplicationMain              : Starting ApplicationMain on 532064-MITLL with PID 8204 (C:\Users\JO24447\workspace\REST_RST_Service\mission-services\route-generator\target\classes started by JO24447 in C:\Users\JO24447\workspace\REST_RST_Service\mission-services\route-generator)
2020-03-10 09:06:48.491  INFO 8204 --- [           main] e.m.l.m.s.r.ApplicationMain              : No active profile set, falling back to default profiles: default
...

有没有人知道需要做什么才能让 eclipse 不使用 Logback 而是使用 Log4J2?

这是父 POM 文件的依赖项部分:

  <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-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
        <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-data-rest</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
        <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-jetty</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    ...

标签: eclipsespring-bootlog4j2

解决方案


您可以检查以下内容

  1. 如果您在 pom.xml 中排除了 spring-boot-starter-logging。
  2. 如果您看到控制台中存在多个绑定,请验证它来自哪些项目。(就我而言,我有一个依赖项目导致重复依赖项)。
  3. 检查 Eclipse 中的引用库是否有任何重复的 jar 引用。您可能会看到存在多个显示的 JAR。您可以删除不需要的引用。

推荐阅读