title: How to bind SLF4J with Log4J?
body: <p>I am trying to setup a project with slf4j + log4j, but the damn thing just doesn't work... I keep getting exception: </p>
  
  <pre><code>Failed to instantiate SLF4J LoggerFactory
  Reported exception:
  java.lang.NoClassDefFoundError: org/apache/log4j/Level
  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
  at TestLog.main(TestLog.java:9)
  Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 6 more
  Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
  at TestLog.main(TestLog.java:9)
  Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 6 more
  </code></pre>
  
  <p>I checked and the class org.apache.log4j.Level is there in my project under "Maven Dependencies > log4j-1.2.17.jar". So why can't the VM find it?</p>
  
  <p>I tried the below options already:</p>
  
  <ol>
  <li>slf4j-api + log4j</li>
  <li>slf4j-api + slf4j-log4j12</li>
  <li>slf4j-api + log4j</li>
  <li>slf4j-api + slf4j-log4j12 + log4j</li>
  </ol>
  
  <p>None of the options worked :(</p>
  
  <p>Here is my currnet pom.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.sample&lt;/groupId&gt;
    &lt;artifactId&gt;DBUnitSample&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
        &lt;version&gt;1.7.7&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
        &lt;version&gt;1.7.7&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.17&lt;/version&gt;
      &lt;/dependency&gt;
  
    &lt;/dependencies&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Also, I noticed that when adding slf4j-log4j12, it adds log4j as a dependency as well in the tree, so I am guessing that adding log4j explicitly here is redundant, but I have added it anyway. Still no luck.</p>
  
  <p>My main code is pretty simple:</p>
  
  <pre><code>import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  public class SampleDao {
      private static final Logger log = LoggerFactory.getLogger(SampleDao.class);
  
      public static void main(String[] args) {
          System.out.println("Hello");
          log.info("Logged");
      }
  }
  </code></pre>
  
  <p>And my log4j.properties:</p>
  
  <pre class="lang-none prettyprint-override"><code>log4j.rootLogger=DEBUG, STDOUT
  log4j.logger.deng=INFO
  log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
  log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
  log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  </code></pre>
  
  <p>Can someone please tell me what am I doing wrong? I already tried searching for answers on SO, but all answers mentioned to add log4j dependency, which I have already added... So not sure, whats wrong.</p>
  
  <p><strong>EDIT</strong>: Here is my entire project structure. I am R-clicking <code>SampleDao</code> and running it as Java application. ( I have also removed dbunit dependency for now, to zero-in on the slf4j problem)
  <img src="https://i.stack.imgur.com/AdwK3.jpg" alt="Maven Project Structure"></p>