title: Log4J2: Modifying Appenders configurations programmatically
body: <p>I want to modify the TraceFileAppender configurations programatically i.e the patternLayout and SizeBasedTriggeringPolicy but somehow my programatic configurations doesn't produce the desired change in the pattern layout and the policy. What can be the possible solution. I am overwriting the same appender programatically. Following is the code.</p>
  
  <h2>XML Configuration File</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration status="WARN"&gt;
      &lt;Appenders&gt;
          &lt;Console name="Console" target="SYSTEM_OUT"&gt;
                  &lt;PatternLayout pattern="%d{DEFAULT} %-5level [%class{1}:%L] - %msg%n"/&gt; 
          &lt;/Console&gt;
          &lt;RollingFile name="TraceFileAppender" fileName="logs/TraceFile.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log"&gt;
          &lt;Policies&gt;
                  &lt;SizeBasedTriggeringPolicy size="50 MB" /&gt;
              &lt;/Policies&gt;
              &lt;PatternLayout pattern="%d{DEFAULT} %-5level [%class{1}:%L] - %msg%n"/&gt; 
          &lt;/RollingFile&gt;
          &lt;RollingFile name="MonitorFileAppender" fileName="logs/MonitorFile.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log"&gt; 
          &lt;Policies&gt;
                  &lt;OnStartupTriggeringPolicy /&gt;
                  &lt;TimeBasedTriggeringPolicy /&gt;
                  &lt;SizeBasedTriggeringPolicy size="50 MB" /&gt;
          &lt;/Policies&gt;
          &lt;PatternLayout pattern="%d{DEFAULT} %-5level [%class{1}:%L] - %msg%n"/&gt;         &lt;/RollingFile&gt; 
      &lt;/Appenders&gt;
      &lt;Loggers&gt;   
          &lt;Root level="INFO"&gt;
              &lt;AppenderRef ref="Console"/&gt;
          &lt;/Root&gt;
      &lt;Logger name="TraceFileLogger"&gt;
          &lt;AppenderRef ref="TraceFileAppender" /&gt;
      &lt;/Logger&gt;
      &lt;Logger name="MonitorFileLogger"&gt;
          &lt;AppenderRef ref="MonitorFileAppender" /&gt;
      &lt;/Logger&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <h2>Main Class</h2>
  
  <pre><code>public class MainClass {
  
      public static void main(String[] args) {
  
          Package1LoggingClass a = new Package1LoggingClass();
          a.performSomeTraceTask();
          a.addAppender();
          a.performSomeTraceTask();   
      }
  }
  </code></pre>
  
  <h2>Package1LoggingClass</h2>
  
  <pre><code>public class Package1LoggingClass {
  
      public void performSomeTraceTask(){
  
          Logger logger = LogManager.getLogger("TraceFileLogger");
  
          logger.info("User info updated");
  
          logger.debug("This is a debug Trace message");
          logger.info("This is an info Trace message");
          logger.warn("This is a warn Trace message");
          logger.error("This is an error Trace message");
          logger.fatal("This is a fatal Trace message");
  
      }
  
      public void addAppender() {
          LoggerContext ctx = (LoggerContext) LogManager.getContext();
          Configuration config = ctx.getConfiguration();
  
          PatternLayout layout = PatternLayout.newBuilder()
            .withConfiguration(config)
            .withPattern("%d{HH:mm:ss.SSS} [Zuraiz] %msg%n")
            .build();
  
  
          RollingFileAppender appender = RollingFileAppender.newBuilder()
            .setConfiguration(config)
            .withName("TraceFileAppender")
            .withLayout(layout)
            .withFileName("logs/TraceFile.log")
            .withFilePattern("Zuzi")
            .withPolicy(SizeBasedTriggeringPolicy.createPolicy("10KB"))
            .build();
  
          appender.start();
          config.addAppender(appender);
          updateLoggers(appender,config);
      }
      private void updateLoggers(final Appender appender, final Configuration config) {
  
          LoggerConfig loggerConfig = config.getLoggerConfig("TraceFileLogger") ;
          loggerConfig.addAppender(appender, null, null);
  
      }
  }
  </code></pre>
  
  <h2>Log File Output</h2>
  
  <pre><code>2018-05-11 17:28:51,023 INFO  [Package1LoggingClass:31] - User info updated
  2018-05-11 17:28:51,027 INFO  [Package1LoggingClass:34] - This is an info Trace message
  2018-05-11 17:28:51,028 WARN  [Package1LoggingClass:35] - This is a warn Trace message
  2018-05-11 17:28:51,028 ERROR [Package1LoggingClass:36] - This is an error Trace message
  2018-05-11 17:28:51,028 FATAL [Package1LoggingClass:37] - This is a fatal Trace message
  2018-05-11 17:28:51,115 INFO  [Package1LoggingClass:31] - User info updated
  2018-05-11 17:28:51,115 INFO  [Package1LoggingClass:34] - This is an info Trace message
  2018-05-11 17:28:51,115 WARN  [Package1LoggingClass:35] - This is a warn Trace message
  2018-05-11 17:28:51,115 ERROR [Package1LoggingClass:36] - This is an error Trace message
  2018-05-11 17:28:51,116 FATAL [Package1LoggingClass:37] - This is a fatal Trace message
  </code></pre>
  
  <p>We can see that [Zuraiz] is not printed in log file. 
  Any solution to this problem? </p>