【问题标题】:Log4J2 - Logging not occurring when run from EclipseLog4J2 - 从 Eclipse 运行时不发生日志记录
【发布时间】:2020-06-22 10:32:15
【问题描述】:
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>
    ...

【问题讨论】:

  • 使用mvn dependency:tree检查哪个库正在使用logback并使用相应的排除
  • 我已经这样做了。它不会出现在树中。
  • 您的 Maven POM 或 Gradle 脚本(只是依赖项部分)看起来如何?应用程序的行为必须相同,否则 Linux 中的“启动脚本”会添加/删除您不知道的 something。另外,你在哪里有 Log4J 2.x 的配置文件?
  • src/main/resources/log4j2-spring.xml.

标签: eclipse spring-boot log4j2


【解决方案1】:

您可以检查以下事项

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2017-12-20
    • 2014-10-30
    • 2017-05-18
    相关资源
    最近更新 更多