3

I setup a basic Java program. I am following this tutorial and have this exact code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

I have the jars slf4j-api-1.7.5.jar and slf4j-log4j12-1.7.5 jar on my build path. I do not understand what gives, the getLogger method exists in the LoggerFactory class which I can F3 (source code look-up) to. I Googled about this and appear to be the only dope with this problem. Any ideas?

Here is my .classpath for Eclipse:

<xml version="1.0" encoding="UTF-8"?>
<classpath>
   <classpathentry kind="src" path="src"/>
   <classpathentry kind="con" path="org.eclipse.jdit..../>
   <classpathentry kind="lib" path="/home/Desktop/slf4j-api-1.7.5.jar" sourcepath="/home/Desktop/slf4j-api-1.7.5.jar"/>
   <classpathentry kind="lib" path="slf4j-log4j12-1.7.5.jar"/>
   <classpathentry kind="lib" path="log4j-1.2.17.jar"/>
   <classpathentry kind="output" path="bin"/>
</classpath>
Laurel
  • 5,965
  • 14
  • 31
  • 57
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • When do you get the error? Does Eclipse mark your uses of LoggerFactory (and is `LoggerFactor` in the title a typo?)? Or do you get the error during runtime? – sheltem Feb 28 '14 at 19:01
  • If Eclipse can not resolve the class, it is not part of the build path. Go to the properties of your project (by right-clicking it and select properties), then choose "Java build path" and switch to the "Libraries" tab. Is there an entry that points to the slf4j-api-1.7.5.jar? If not, add it. If it is... then that's extremely odd and showing us the .classpath-file of your project might help. – sheltem Feb 28 '14 at 19:13
  • I added my .classpath above – smuggledPancakes Feb 28 '14 at 20:18

5 Answers5

6

On the tutorial page you link to, there is the following note:

slf4j-log4j12-1.7.6.jar

Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.

Did you include log4j.jar?

David Hodgson
  • 854
  • 8
  • 13
  • I added log4j.jar to my build path but there is still no change. I do not have access to the 1.7.6 slf4j jars on my development box. Should that 0.0.1 difference be causing this error? I can see the method call in my jar's source code. – smuggledPancakes Feb 28 '14 at 17:36
  • 1
    Doesn't matter which concrete logging framework is to be used in the end, Logger and LoggerFactory are classes that reside in the slf4j-api. A missing binding or logging implementation would lead to a completely different error. – sheltem Feb 28 '14 at 19:00
  • I had a spelling error on my path, sorry about this mess – smuggledPancakes Feb 28 '14 at 20:54
4

My problem was solved after the inclusion of

slf4j-api-1.7.7.jar and slf4j-simple-1.7.7.jar

on classpath.

Alan CN
  • 1,467
  • 1
  • 13
  • 13
1

For me, when I added the Maven dependency below, it worked:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
Thanuja
  • 11
  • 2
0

You can always use the logger statically instead of using loggerfactory and creating an instance every time you need it:

final static Logger logger = Logger.getLogger(HelloWorld.class);

According to this SO answer, there does not seem to be much overhead of using either way:

What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?

Community
  • 1
  • 1
ephemeralCoder
  • 187
  • 2
  • 10
0

I solved it with

import org.apache.log4j.Logger;

instead of

import java.util.logging.Logger;
Mark
  • 143,421
  • 24
  • 428
  • 436
yaghob abbasi
  • 96
  • 1
  • 12