title: Log4j2 custom layout Patterns
body: <p>Hi I am migrating from log4j to log4j2. </p>
  
  <p>In my code I found some custom patterns class are used. I am not sure how to implement the same in log4j2. 
  Can anybody help me on this? </p>
  
  <p>Here is the code I'm using</p>
  
  <pre><code>public class MyPatternLayout extends PatternLayout
  {
      protected PatternParser createPatternParser(String pattern)
      {
          return new MyPatternParser(pattern);
      }
  }
  
  public class MyPatternParser extends PatternParser
  {
      private static class MyPatternConverter extends PatternConverter
      {
  
          int type = 0;
  
          public String convert(LoggingEvent event)
          {
              String converted = "";
              try
              {
                  switch (type)
                  {
                      case 33: // 'I' - Requiest Unique ID
                          UniqueId id = (UniqueId) ActParam.getParameter(LoggerParams.activityId);
                          if (id == null)
                          {
                              id = new UniqueId();
                              ActParam.setParameter(LoggerParams.activityId, id);
                          }
                          converted = id.getId();
                          break;
                  }
              }
              catch (Exception ex)
              {
                  LogLog.error("Error processing activityId.", ex);
              }
              return converted;
          }
  
          MyPatternConverter(FormattingInfo formattingInfo, int type)
          {
              super(formattingInfo);
              this.type = type;
          }
      }
  
      public MyPatternParser(String pattern)
      {
          super(pattern);
  
      }
  
      protected void finalizeConverter(char c)
      {
          PatternConverter pc = null;
          switch (c)
          {
              case 33: 
              case 53: 
                  pc = new MyPatternConverter(formattingInfo, c);
                  currentLiteral.setLength(0);
  
                  addConverter(pc);
                  break;
              default:
                  super.finalizeConverter(c);
                  break;
          }
  
      }
  
  }
  </code></pre>
  
  <p>Thanks in Advance.</p>
  
  <p>After the suggestions to change to @plugins, below is my code.</p>
  
  <pre><code>@Plugin(name = "MyPatternLayout", category = "Converter")
  @ConverterKeys(
  {
          "zs"
  })
  public class MyPatternLayout extends LogEventPatternConverter
  {
      protected MyPatternLayout(String name, String style)
      {
          super(name, style);
      }
  
      public static MyPatternLayout newInstance(String[] options)
      {
          return new MyPatternLayout("zs", Thread.currentThread().getName());
      }
  
      @Override
      public void format(LogEvent event, StringBuilder toAppendTo)
      {
  
          toAppendTo.append(getTransactionId());
      }
      protected String getTransactionId()
      {
  
          String converted = "";
          try
          {
               UniqueId id = (UniqueId) ThreadParameter.getParameter(LoggerThreadParameters.activityId);
              if (id == null)
              {
                  id = new UniqueId();
                  ThreadParameter.setParameter(LoggerThreadParameters.activityId, id);
              }
              converted = id.getId();
  
          }
          catch (Exception ex)
          {
  
              StatusLogger.getLogger().error("Error processing activityId.", ex);
          }
          return converted;
      }
  
  }
  </code></pre>
  
  <p>and the log4j2.xml is:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration status="trace" packages="com.metavante.efd.cms.log.log4j.EFDPatternLayout"&gt;
         &lt;Appenders&gt;
      &lt;Console name="STDOUT" target="SYSTEM_OUT"&gt;
       &lt;PatternLayout pattern="%p|%d{ISO8601}|%c|%m|%zs%n"/&gt;
  
      &lt;/Console&gt;
    &lt;/Appenders&gt;
    &lt;loggers&gt;
       &lt;Logger name="org.apache.log4j.xml" level="all" /&gt;
       &lt;root level="all"&gt;
              &lt;appender-ref ref="STDOUT" level="TRACE" /&gt;
        &lt;/root&gt;
    &lt;/loggers&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>and the output in the log is like blow.</p>
  
  <pre><code>2017-07-06 10:55:32,137 main DEBUG Initializing configuration XmlConfiguration[location=C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml]
  2017-07-06 10:55:32,152 main DEBUG Installed script engines
  2017-07-06 10:55:32,652 main DEBUG Oracle Nashorn Version: 1.8.0_121, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
  2017-07-06 10:55:32,659 main DEBUG Took 0.005574 seconds to load 0 plugins from package com.log4j.MyPatternLayout
  2017-07-06 10:55:32,659 main DEBUG PluginManager 'Core' found 112 plugins
  2017-07-06 10:55:32,659 main DEBUG PluginManager 'Level' found 0 plugins
  2017-07-06 10:55:32,665 main DEBUG PluginManager 'Lookup' found 13 plugins
  2017-07-06 10:55:32,668 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
  2017-07-06 10:55:32,686 main TRACE TypeConverterRegistry initializing.
  2017-07-06 10:55:32,687 main DEBUG PluginManager 'TypeConverter' found 26 plugins
  2017-07-06 10:55:32,708 main DEBUG PatternLayout$Builder(pattern="%p|%d{ISO8601}|%c|%m|%zs%n", PatternSelector=null, Configuration(C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
  2017-07-06 10:55:32,709 main DEBUG PluginManager 'Converter' found 41 plugins
  2017-07-06 10:55:32,733 main ERROR Unrecognized format specifier [zs]
  2017-07-06 10:55:32,734 main ERROR Unrecognized conversion specifier [zs] starting at position 24 in conversion pattern.
  2017-07-06 10:55:32,734 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
  2017-07-06 10:55:32,748 main DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%p|%d{ISO8601}|%c|%m|%zs%n), name="STDOUT", Configuration(C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml), Filter=null)
  2017-07-06 10:55:32,758 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
  2017-07-06 10:55:32,758 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
  2017-07-06 10:55:32,759 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
  2017-07-06 10:55:32,761 main DEBUG createAppenders(={STDOUT})
  2017-07-06 10:55:32,762 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
  2017-07-06 10:55:32,771 main DEBUG createLogger(additivity="true", level="ALL", name="org.apache.log4j.xml", includeLocation="null", ={}, ={}, Configuration(C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml), Filter=null)
  2017-07-06 10:55:32,776 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
  2017-07-06 10:55:32,777 main DEBUG createAppenderRef(ref="STDOUT", level="TRACE", Filter=null)
  2017-07-06 10:55:32,777 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
  2017-07-06 10:55:32,778 main DEBUG createLogger(additivity="null", level="ALL", includeLocation="null", ={STDOUT}, ={}, Configuration(C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml), Filter=null)
  2017-07-06 10:55:32,779 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
  2017-07-06 10:55:32,781 main DEBUG createLoggers(={org.apache.log4j.xml, root})
  2017-07-06 10:55:32,783 main DEBUG Configuration XmlConfiguration[location=C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml] initialized
  2017-07-06 10:55:32,783 main DEBUG Starting configuration XmlConfiguration[location=C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml]
  2017-07-06 10:55:32,784 main DEBUG Started configuration XmlConfiguration[location=C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml] OK.
  2017-07-06 10:55:32,784 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@51931956...
  2017-07-06 10:55:32,785 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
  2017-07-06 10:55:32,785 main TRACE DefaultConfiguration stopping root LoggerConfig.
  2017-07-06 10:55:32,785 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
  2017-07-06 10:55:32,786 main TRACE DefaultConfiguration stopping remaining Appenders.
  2017-07-06 10:55:32,786 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
  2017-07-06 10:55:32,786 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
  2017-07-06 10:55:32,786 main DEBUG Appender DefaultConsole-1 stopped with status true
  2017-07-06 10:55:32,787 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
  2017-07-06 10:55:32,787 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
  2017-07-06 10:55:32,787 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@51931956 OK
  2017-07-06 10:55:32,790 main TRACE Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@71c27ee8
  2017-07-06 10:55:32,790 main TRACE Reregistering context (1/1): '18b4aac2' org.apache.logging.log4j.core.LoggerContext@3e7dd664
  2017-07-06 10:55:32,791 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2'
  2017-07-06 10:55:32,791 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger'
  2017-07-06 10:55:32,792 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector'
  2017-07-06 10:55:32,792 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*'
  2017-07-06 10:55:32,793 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=*'
  2017-07-06 10:55:32,793 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncAppenders,name=*'
  2017-07-06 10:55:32,794 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncLoggerRingBuffer'
  2017-07-06 10:55:32,794 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*,subtype=RingBuffer'
  2017-07-06 10:55:32,799 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2
  2017-07-06 10:55:32,802 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger
  2017-07-06 10:55:32,805 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector
  2017-07-06 10:55:32,807 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=
  2017-07-06 10:55:32,808 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=org.apache.log4j.xml
  2017-07-06 10:55:32,812 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=STDOUT
  2017-07-06 10:55:32,818 main TRACE Using default SystemClock for timestamps.
  2017-07-06 10:55:32,819 main TRACE Using DummyNanoClock for nanosecond timestamps.
  2017-07-06 10:55:32,819 main DEBUG Reconfiguration complete for context[name=18b4aac2] at URI C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\xml\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@3e7dd664) with optional ClassLoader: null
  2017-07-06 10:55:32,820 main DEBUG Shutdown hook enabled. Registering a new one.
  2017-07-06 10:55:32,822 main DEBUG LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@3e7dd664] started OK.
  DEBUG|2017-07-06T10:55:32,836|Default.com.JBossStartupClass|deleting directory: C:\eclipse\workspace\test\test.ear\test.war\WEB-INF\templates|%zs
  2017-07-06 10:55:32,843 main DEBUG AsyncLogger.ThreadNameStrategy=CACHED
  2017-07-06 10:55:32,843 main TRACE Using default SystemClock for timestamps.
  </code></pre>
  
  <p>and I think somehow it is not finding the plugin. Please help me.
  Note: I am working with JBoss EAP 7.</p>
  
  <p>and when I use log4j2.proeprties file instead of logj2.xml it is giving different error in the log.</p>
  
  <pre><code>name=PropertiesConfig
  property.filename =logs
  appenders =console
  appender.console.type =Console
  appender.console.name =STDOUT
  appender.console.layout.type =com.log4j.MyPatternLayout
  appender.console.layout.pattern =%p|%d{ISO8601}|%c|%m|%zs%n
  
  rootLogger.level =debug
  rootLogger.appenderRefs =stdout
  rootLogger.appenderRef.stdout.ref =STDOUT
  </code></pre>
  
  <p>and below is the log in the console when I use .proeprties file.</p>
  
  <pre><code>2017-07-06 11:02:48,374 main ERROR Unable to locate plugin type for com.metavante.efd.cms.log.log4j.EFDPatternLayout
  2017-07-06 11:02:48,433 main ERROR Unable to locate plugin for com.metavante.efd.cms.log.log4j.EFDPatternLayout
  2017-07-06 11:02:48,455 main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.ConsoleAppender, element Console. java.lang.NullPointerException
      at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.findNamedNode(PluginElementVisitor.java:103)
      at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:87)
      at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:179)
      at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:121)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
      at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
      at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
      at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
      at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
      at com.metavante.efd.util.logging.logStore.types.Log4jLogStore.&lt;init&gt;(Log4jLogStore.java:34)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.metavante.efd.util.logging.LoggerProperties.getLogStore(LoggerProperties.java:148)
      at com.metavante.efd.util.logging.Logger.getLogStoreInstance(Logger.java:97)
      at com.metavante.efd.util.logging.Logger.debug(Logger.java:155)
      at com.fis.customAgent.local.JBoss_Startup_Agent.cleanDirectories(JBoss_Startup_Agent.java:220)
      at com.fis.customAgent.local.JBoss_Startup_Agent.premain(JBoss_Startup_Agent.java:182)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
  
  2017-07-06 11:02:48,461 main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console. java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.ConsoleAppender
      at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:224)
      at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:130)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
      at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
      at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
      at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
      at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
      at com.metavante.efd.util.logging.logStore.types.Log4jLogStore.&lt;init&gt;(Log4jLogStore.java:34)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.metavante.efd.util.logging.LoggerProperties.getLogStore(LoggerProperties.java:148)
      at com.metavante.efd.util.logging.Logger.getLogStoreInstance(Logger.java:97)
      at com.metavante.efd.util.logging.Logger.debug(Logger.java:155)
      at com.fis.customAgent.local.JBoss_Startup_Agent.cleanDirectories(JBoss_Startup_Agent.java:220)
      at com.fis.customAgent.local.JBoss_Startup_Agent.premain(JBoss_Startup_Agent.java:182)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
      at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
  
  2017-07-06 11:02:48,463 main ERROR Null object returned for Console in Appenders.
  2017-07-06 11:02:48,464 main ERROR Unable to locate appender "STDOUT" for logger config "root"
  </code></pre>
  
  <p>Please help me in this. Thank you.</p>