slf4j & log4j2 maven setup query

自古美人都是妖i 提交于 2019-12-18 12:49:22

问题


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 copied below with this pom file - any idea what I need to add/remove to get this to work. I have already visited the url in the error as well as the log4j2 dependencies page so please do not just point to URLs in your response.

Message:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

pom file

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <slf4j.version>1.7.7</slf4j.version>
</properties>

   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>


    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.2.0</version>
    </dependency>

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

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

Update: I added the following dependency to my pom file and I see the jar in my mavenrepository - though I still see the same message when I run mvn clean/install

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

回答1:


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>



回答2:


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.




回答3:


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




回答4:


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>


来源:https://stackoverflow.com/questions/25386651/slf4j-log4j2-maven-setup-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!