29

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>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114

4 Answers4

26

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>
Nicholas
  • 2,147
  • 3
  • 23
  • 31
ali haider
  • 19,175
  • 17
  • 80
  • 149
  • 1
    The groupId should be "org.apache.logging.log4j" and the version "2.0.1", as described here: http://logging.apache.org/log4j/2.x/maven-artifacts.html – sheltem Aug 19 '14 at 15:18
  • 1
    my mistake - yes, the version should be 2.0.1 not the beta one and not with the adapters group id – ali haider Aug 19 '14 at 16:14
  • 2
    @user3813256 then why did you mark the answer correct if it did not solve the issue? – Danny Bullis Apr 06 '18 at 19:06
10

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.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I still see the same message when I run maven clean/install via eclipse. I seem to have all the dependencies in my maven repo - slf4j=api-1.7.7, log4j-slf4j-impl-2.0.1, log4j-api-2.0.1, log4j-core-2.0.1 but I get the message when I run maven and my log file output does not get generated. I get the file output with the same log4j2 xml file when I do not use maven but have all the jars in my lib directory and use eclipse for the build path –  Aug 19 '14 at 16:12
  • @user3813256: I hoped it would be enough because I do not use Eclipse, but I've updated my answer with references to another post. – Serge Ballesta Aug 19 '14 at 16:26
  • I'll accept as soon as I can verify - I think you are right but I need to prove it on my computer - thank you for your help! –  Aug 19 '14 at 17:00
1

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

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
-1

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>
Ali
  • 2,702
  • 3
  • 32
  • 54