title: Logback - get method name
body: <p>We are currently migrating from log4j to Logback, but we are having problems obtaining the "original" method name that triggered the log.</p>
  
  <p>I'm calling it "original" because we have a centralized logger class (to hide and manipulate certain logs) and what's showing up in the logs is the method name from that centralized class.</p>
  
  <p>In log4j we were able to obtain the "original" method name correctly.</p>
  
  <p>Is Logback able to obtain it or not?</p>
  
  <p><strong>Loggers parameters</strong>:</p>
  
  <p><strong>log4j</strong></p>
  
  <pre><code>&lt;param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} %5p [%F] - %M() - %m%n"/&gt;
  </code></pre>
  
  <p><strong>Logback</strong></p>
  
  <pre><code>&lt;pattern&gt;%d{"dd-MM-yyyy HH:mm:ss,SSS"} %-5level [%logger - %M] - %msg%n&lt;/pattern&gt;
  </code></pre>
  
  <p><strong>Results: (Method Name - Class Name)</strong></p>
  
  <p><strong>log4j</strong></p>
  
  <pre><code>doLogTester1 - a.Tester1            
  doLogTester2 - b.Tester2            
  doLogTester1 - a.Tester1            
  doLogTester2 - b.Tester2            
  </code></pre>
  
  <p><strong>Logback</strong></p>
  
  <pre><code>processLog - a.Tester1              
  processLog - b.Tester2              
  processLog - a.Tester1              
  processLog - b.Tester2              
  </code></pre>
  
  <p><strong>EDIT</strong>
   - Full example</p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>public class Main
  {
  private static final LoggerCommon logger = new LoggerCommon(Main.class);
  
      public static void main(String[] args)
      {
          logger.doLog("I'm on the Main class in the main method");
      }
  }
  </code></pre>
  
  <p><strong>LoggerCommon</strong></p>
  
  <p>log4j</p>
  
  <pre><code>import org.apache.log4j.Level;
  import org.apache.log4j.Logger;
  
  public class LoggerCommon
  {
      private static Logger logger;
  
      public LoggerCommon(Class&lt;?&gt; c)
      {
          logger = Logger.getLogger(c);
      }
  
      public void doLog(String message)
      {
          logger.log(LoggerCommon.class.getName(), Level.INFO, message, null);
      }
  }
  </code></pre>
  
  <p>logback</p>
  
  <pre><code>import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  public class LoggerCommon
  {
      private Logger logger;
  
      public LoggerCommon(Class&lt;?&gt; c)
      {
          logger = LoggerFactory.getLogger(c);
      }
  
      public void doLog(String message)
      {
          logger.info(message);
      }
  }
  </code></pre>
  
  <p><strong>Config's</strong></p>
  
  <p>log4j</p>
  
  <pre><code>&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
      &lt;appender name="CA" class="org.apache.log4j.ConsoleAppender"&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%M - %F - %m%n"/&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
      &lt;root&gt;
          &lt;level value="debug"/&gt;
          &lt;appender-ref ref="CA"/&gt;
      &lt;/root&gt;
  &lt;/log4j:configuration&gt;
  </code></pre>
  
  <p>logback</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;appender name="CA" class="ch.qos.logback.core.ConsoleAppender"&gt;
          &lt;encoder&gt;
              &lt;pattern&gt;%M - %logger - %msg %n&lt;/pattern&gt;
          &lt;/encoder&gt;
      &lt;/appender&gt;
      &lt;root level="TRACE"&gt;
          &lt;appender-ref ref="CA"/&gt;
      &lt;/root&gt;
  &lt;/configuration&gt;
  </code></pre>