slf4j & log4j2 maven setup query

后端 未结 4 1714
梦谈多话
梦谈多话 2020-12-30 22:35

I am using log4j2 and slf4j in my project & using maven for the build. I am using the following pom file (releveant dependencies shown only) but I am getting the error

相关标签:
4条回答
  • 2020-12-30 23:09

    I think your first pom.xml is correct, (dependency is correct), maybe config file location is wrong

    pom.xml

          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
    
    0 讨论(0)
  • 2020-12-30 23:16

    Your log4j2 configuration in correct (POM side), but you never say to slf4j where it should write (backend part).

    Your should add that to your pom file

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0.1</version>
    </dependency>
    

    It is the Log4j 2 SLF4J Binding. According to Log4j 2 SLF4J Binding documentation The Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation

    If it still does not work, you may have an Eclipse problem because Eclipse m2e is known to be weird regarding slf4j. According to this detailed post from SO SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. error a workaround could be to use an external maven to do the build.

    0 讨论(0)
  • 2020-12-30 23:30

    Apart from the log4j-slf4j-impl dependency, you also need the slf4j-ext dependency.

    See http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/dependencies.html

    0 讨论(0)
  • You seem to be missing the following from your pom file.

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0.1</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题