title: Logback - Layout & Pattern in logback.xml
body: <p>I am using logback with slf4j for logging in Spring Boot application. I have created a custom layout class because all log statements are to be wrapped as a json. I have configured the logback-spring.xml as show below to take the custom layout. It works! </p>
  
  <p>Issue is I am unable to apply the pattern. Only either the layout works (or) the pattern. What I want is always on log, go to layout class and then apply the pattern before logging.</p>
  
  <pre><code>&lt;appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
      &lt;file&gt;${user.home}/logs/sample.log&lt;/file&gt;
      &lt;rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"&gt;
          &lt;fileNamePattern&gt;${user.home}/logs/sample_%d{yyyy-MM-dd}.%i.log&lt;/fileNamePattern&gt;
  
          &lt;timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"&gt;
              &lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;
          &lt;/timeBasedFileNamingAndTriggeringPolicy&gt;
          &lt;!-- how many days to keep the files --&gt;
          &lt;maxHistory&gt;30&lt;/maxHistory&gt;
      &lt;/rollingPolicy&gt;
  
      &lt;encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"&gt;
          &lt;layout class="com.test.test.payment.core.logging.SampleLogLayout" &gt;
          &lt;/layout&gt;
      &lt;/encoder&gt;
      *&lt;!-- &lt;encoder&gt;
          &lt;charset&gt;UTF-8&lt;/charset&gt;
          &lt;Pattern&gt;{"@timestamp": "%d{yyyy-MM-dd HH:mm:ss.SSS}", "priority": "%p", "application": "payment",
              "class": "%C", "file": "%F:%L", "payload": %m }%n
          &lt;/Pattern&gt;
      &lt;/encoder&gt;--&gt;*
  &lt;/appender&gt;
  </code></pre>
  
  <p>Here's the <code>SampleLogLayout</code> class:</p>
  
  <pre><code>public class SampleLogLayout extends LayoutBase&lt;LoggingEvent&gt; {
  
      @Override
      public String doLayout(LoggingEvent event) {
  
          String renderedMessage = event.getMessage();
  
          if (!isJson(renderedMessage)) {
              Throwable throwable = null;
  
              if (event.getLevel().equals(Level.ERROR) || event.getLevel().equals(Level.WARN)) {
  
              final IThrowableProxy throwableProxy = event.getThrowableProxy();
              if ((throwableProxy != null) &amp;&amp; (throwableProxy instanceof ThrowableProxy)) {
                  ThrowableProxy proxy = (ThrowableProxy) throwableProxy;
                  throwable = proxy.getThrowable();
              }
              String message = LogErrorMessage.create(CommonCoreErrors.GENERIC_ERROR)
                      .message(renderedMessage).exception(throwable).build();
  
              return message;
          } else {
                  return LogMessage.create(renderedMessage).build();
          }
  
          }
          return renderedMessage;
      }
  
      private boolean isJson(String msg) {
          if (null == msg) {
              return false;
          } else {
              return msg.startsWith("{") &amp;&amp; msg.endsWith("}");
          }
      }
  }
  </code></pre>