简体   繁体   English

使用log4j2和logback

[英]Using log4j2 and logback

I have an app using Log4j2 and so far I have no issues. 我有一个使用Log4j2的应用程序,到目前为止,我没有任何问题。 This morning, I added a 3rd party library which , in turn, uses logback for logging. 今天早上,我添加了一个3rd party库,该库又使用logback进行日志记录。

Now I see this error: 现在,我看到此错误:

Error loading configuration  Cause: org.apache.logging.slf4j.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext

How do I fix this error? 如何解决此错误?

My app has the following dependencies: 我的应用程序具有以下依赖性:

  <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-api</artifactId>
          <version>2.4.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
          <version>2.4.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-slf4j-impl</artifactId>
          <version>2.4.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-jcl</artifactId>
          <version>2.4.1</version>
      </dependency>

Whereas the 3rd party library uses the following: 而第三方库使用以下内容:

 <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-ext</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.12</version>
        </dependency>

Is it possible to have both log4j2 and logback configured at the same time? 是否可以同时配置log4j2和logback? How do I fix the configuration? 如何修复配置?

Logbook uses SLF4J as its API. 日志使用SLF4J作为其API。 You cannot have both Log4j-slf4j-impl and the Logback jars. 您不能同时拥有Log4j-slf4j-impl和Logback jar。 If you do not want to use Logback then where you have the dependency declared for the 3rd party library you need to add exclusions for logback-classic and logback-core. 如果您不想使用Logback,则在声明了第3方库的依赖项的地方,需要为logback-classic和logback-core添加排除项。

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

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