title: Creating a custom layout in Logback
body: <p>I am trying to create a custom layout in logback as shown in an example in <a href="http://logback.qos.ch/xref/chapters/layouts/MySampleLayout.html" rel="noreferrer">chapter 6 in the manual</a>:</p>
  
  <pre><code>package com.dces.util;
  
  import ch.qos.logback.classic.spi.ILoggingEvent;
  import ch.qos.logback.core.CoreConstants;
  import ch.qos.logback.core.LayoutBase;
  
  public class LoggingConsoleLayout extends LayoutBase&lt;ILoggingEvent&gt;{
  
  @Override
  public String doLayout(ILoggingEvent event) {
      StringBuffer sbuf = new StringBuffer(128);
      sbuf.append("-- ");
      sbuf.append("[");
      sbuf.append(event.getLevel());
      sbuf.append("]");
      sbuf.append(event.getLoggerName());
      sbuf.append(" - ");
      sbuf.append(event.getFormattedMessage().replaceAll("\n", "\n\t"));
      sbuf.append(CoreConstants.LINE_SEPARATOR);
      return sbuf.toString();
  }
  </code></pre>
  
  <p>Then I configured the XML file as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
  
  &lt;appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"&gt;
      &lt;encoder&gt;
          &lt;!-- &lt;pattern&gt; %-5level [%logger{0}] - %msg%n &lt;/pattern&gt; --&gt;
           &lt;layout class="com.dces.util.LoggingConsoleLayout" /&gt;
      &lt;/encoder&gt;
  &lt;/appender&gt;
  
  &lt;root level="trace"&gt;
      &lt;appender-ref ref="STDOUT" /&gt;
  &lt;/root&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>However, I get the following error when I try to create a logger as following:</p>
  
  <pre><code>public class Main {
       private static Logger logger = (Logger)LoggerFactory.getLogger(Main.class);
       public static void main(String [] args)
       {
           logger.info("Testing");
       }
  }
  </code></pre>
  
  <p>Here is the error message that I get:</p>
  
  <pre><code>05:40:11,519 |-ERROR in ch.qos.logback.core.joran.util.PropertySetter@3462fc6c - Could not invoke method setLayout in class ch.qos.logback.classic.encoder.PatternLayoutEncoder with parameter of type com.dces.util.LoggingConsoleLayout java.lang.reflect.InvocationTargetException
  at java.lang.reflect.InvocationTargetException
  at  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at  at java.lang.reflect.Method.invoke(Unknown Source)
  at  at ch.qos.logback.core.joran.util.PropertySetter.invokeMethodWithSingleParameterOnThisObject(PropertySetter.java:293)
  at  at ch.qos.logback.core.joran.util.PropertySetter.setComplexProperty(PropertySetter.java:356)
  at  at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:179)
  at  at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:318)
  at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:197)
  at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:183)
  at  at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
  at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:157)
  at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:143)
  at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:106)
  at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:56)
  at  at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
  at  at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
  at  at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
  at  at org.slf4j.impl.StaticLoggerBinder.&lt;clinit&gt;(StaticLoggerBinder.java:55)
  at  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
  at  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
  at  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
  at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
  at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
  at  at com.dces.Main.&lt;clinit&gt;(Main.java:8)
  Caused by: java.lang.UnsupportedOperationException: one cannot set the layout of ch.qos.logback.classic.encoder.PatternLayoutEncoder
  at  at ch.qos.logback.core.pattern.PatternLayoutEncoderBase.setLayout(PatternLayoutEncoderBase.java:64)
  at  ... 25 common frames omitted
  05:40:11,538 |-ERROR in ch.qos.logback.classic.PatternLayout("null") - Empty or null pattern.
  </code></pre>