title: How to log with SLF4J/logback in Tomcat + Hibernate
body: <p>So, I'm deploying a web app which use Hibernate 4.3.6. and Tomcat 7.
  I'm trying to use SLF4J as logging abstract layer to log. However, I have many issues and doubts.
  First of all, both Hibernate and Tomcat come yet with different logging frameworks, jboss-logging and juli respectively (even if I can't find juli on the libraries)
  So, I think I have to install the bridge from these frameworks to SLF4J. 
  For Hibernate, from <a href="https://stackoverflow.com/a/19488546/2326627">this</a> answer, a solution could be to use a SystemProperty. However, if I don't set this, </p>
  
  <blockquote>
    <p>it tries jboss, then log4j, then slf4j (only if logback used) and fallback to jdk.
    So, maybe this is what I want: don't set the SystemProperty and leaves it choose slf4j.</p>
  </blockquote>
  
  <p>For Tomcat, I think I have to simply add the jul-to-slf4j and tomcat-juli jars to the build path.
  So, at this moment, I have this jars on the build path:</p>
  
  <ul>
  <li>slf4j-api-1.7.7</li>
  <li>logback-classic-1.1.2</li>
  <li>logback-core-1.1.2</li>
  <li>tomcat-juli-7.0.42</li>
  <li>jul-to-slf4j-1.7.7</li>
  </ul>
  
  <p>(I've deleted jboss-logging and the default and old slf4j coming with hibernate, but not tomcat-logging)
  Also, I've added this xml file inside a package in my project.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration debug = "true"&gt;
  
      &lt;appender name="FILE" class="ch.qos.logback.core.FileAppender"&gt;
          &lt;file&gt;/var/lib/tomcat7/MyApp/MyLog.log&lt;/file&gt;
          &lt;encoder&gt;
              &lt;Pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
              &lt;/Pattern&gt;
          &lt;/encoder&gt;
      &lt;/appender&gt;
  
      &lt;root level="trace"&gt;
      &lt;appender-ref ref="FILE" /&gt;
      &lt;/root&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>but I haven't linked it to any resource at all. Have I to declare this log configuration somewhere in web.xml?
  To call SLF4J inside one of my resources, I simply put these lines</p>
  
  <pre><code>import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  ...
  private final static Logger LOG = LoggerFactory.getLogger(MyServlet.class);
  ...
  LOG.debug("I'm logging");
  </code></pre>
  
  <p>However, nothing works. I continue to have messages from Tomcat logger, while Hibernate complains that it doesn't have a logger; at the same time, I can't see any log coming from slf4j/logback. </p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class pervasive.com.gmail.tigerjack89.forum.server.controller.servlet.ContainerContext
  java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2944)
      at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1208)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
      at org.hibernate.cfg.Configuration.&lt;clinit&gt;(Configuration.java:192)
      ...
  Caused by: java.lang.ClassNotFoundException: org.jboss.logging.BasicLogger
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
  </code></pre>
  
  <p>The error message stay the same even if I add "-Dorg.jboss.logging.provider=slf4j" to the VM arguments. 
  So, how can I solve the problem?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Adding back jboss-logging to classpath the error disappeared. Problem is that now I have a lot of messages coming from logback, jboss-logging and juli. Also, no file is created until now, even if I specified one in logback.xml; instead, it continues to write to the console using the default pattern (indeed, it doesn't print the day as specified in logback.xml).
  This is an example of log output, where you can easily identify messages coming from the different loggers (logback start with the hour, jboss-logging start with Hibernate: and juli start with the debug level)</p>
  
  <pre><code>16:50:01.331 [localhost-startStop-1] DEBUG org.hibernate.SQL - 
      select
          subforums0_.parent_sub_forum as parent_s3_1_0_,
          subforums0_.id as id1_1_0_,
          subforums0_.id as id1_1_1_,
          subforums0_.parent_sub_forum as parent_s3_1_1_,
          subforums0_.title as title2_1_1_ 
      from
          subforum subforums0_ 
      where
          subforums0_.parent_sub_forum=?
  Hibernate: 
      select
          subforums0_.parent_sub_forum as parent_s3_1_0_,
          subforums0_.id as id1_1_0_,
          subforums0_.id as id1_1_1_,
          subforums0_.parent_sub_forum as parent_s3_1_1_,
          subforums0_.title as title2_1_1_ 
      from
          subforum subforums0_ 
      where
          subforums0_.parent_sub_forum=?
  16:50:01.332 [localhost-startStop-1] DEBUG o.h.l.p.e.p.i.ResultSetProcessorImpl - Preparing collection intializer : [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums#11]
  16:50:01.337 [localhost-startStop-1] DEBUG o.h.e.l.i.CollectionLoadContext - 1 collections were found in result set for role: pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums
  16:50:01.337 [localhost-startStop-1] DEBUG o.h.e.l.i.CollectionLoadContext - Collection fully initialized: [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums#11]
  16:50:01.337 [localhost-startStop-1] DEBUG o.h.e.l.i.CollectionLoadContext - 1 collections initialized for role: pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums
  16:50:01.338 [localhost-startStop-1] DEBUG o.h.l.c.p.AbstractLoadPlanBasedCollectionInitializer - Done loading collection
  16:50:01.338 [localhost-startStop-1] DEBUG o.h.e.t.spi.AbstractTransactionImpl - committing
  16:50:01.339 [localhost-startStop-1] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
  16:50:01.346 [localhost-startStop-1] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
  16:50:01.361 [localhost-startStop-1] DEBUG o.h.engine.internal.Collections - Collection found: [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforumThreads#11], was: [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforumThreads#11] (uninitialized)
  16:50:01.361 [localhost-startStop-1] DEBUG o.h.engine.internal.Collections - Collection found: [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums#11], was: [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum.subforums#11] (initialized)
  16:50:01.362 [localhost-startStop-1] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
  16:50:01.362 [localhost-startStop-1] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 2 collections
  16:50:01.364 [localhost-startStop-1] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
  16:50:01.366 [localhost-startStop-1] DEBUG o.h.internal.util.EntityPrinter - pervasive.com.gmail.tigerjack89.forum.shared.model.entities.Subforum{subforumThreads=&lt;uninitialized&gt;, subforums=[], subforumParent=null, subforumId=11, subforumTitle=Main}
  16:50:01.367 [localhost-startStop-1] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection
  16:50:01.373 [localhost-startStop-1] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
  16:50:01.373 [localhost-startStop-1] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection
  Dec 06, 2014 4:50:01 PM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-bio-8080"]
  Dec 06, 2014 4:50:01 PM org.apache.catalina.startup.Catalina start
  INFO: Server startup in 5850 ms
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Deploying the app on the system installation folder of Tomcat (instead of the one used by Eclipse), all the juli files are written into catalina.[date].log, while the logs coming from jboss-logging and logback are written into catalina.out. It seems that the logback.xml file isn't recognized at all.</p>
  
  <hr>
  
  <p><strong>EDIT 3</strong>
  Well, one of the problems seems to be with the position of logback.xml. I had put it in a subfolder, while it should be directly under WEB-INF/classes. Now it logs to the right directories. However, both Hibernate and Tomcat still continues to write their log messages to System.out (and as a result to catalina.out).</p>
  
  <hr>
  
  <p><strong>EDIT 4</strong>
  Ok, I disabled Hibernate outputs to stdout deleting <code>&lt;property name="show_sql"&gt;true&lt;/property&gt;</code> from <code>hibernate.cfg.xml</code>.
  Now, my question is: is it ok/desiderable that tomcat continues to print messages like</p>
  
  <pre><code>Dec 07, 2014 7:46:59 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-bio-8080"]
  </code></pre>
  
  <p>on the console?</p>