title: Creating log4j Async logger programmatically
body: <p>I am using Log4J 2.10.</p>
  <p>I am trying to create async logger\appender programmatically. I need to have two things:</p>
  <ol>
  <li>I should be able to specify file path for log file at run time.</li>
  <li>I need to specify pattern at run time.</li>
  </ol>
  <p>I could come up with code below. But I cannot add RollingFileAppender to AsyncAppender. There are examples on the net using AsyncAppender.wrap. But that API doesn't seem to be available with Log4J 2.10.</p>
  <p>Do you know how I can achieve this?</p>
  <pre><code>void createLog4JLogger(final String logFilePath) {
      
      LoggerContext context = (LoggerContext) LogManager.getContext();
      final Configuration config = context.getConfiguration();
  
      final PatternLayout patternLayout = PatternLayout.newBuilder().withPattern(CONVERSION_PATTERN).withCharset(Charset.defaultCharset()).build();
  
      final RollingFileAppender fileAppender = 
              RollingFileAppender.newBuilder().withName(APPENDER_NAME).withLayout(patternLayout).withFileName(logFilePath).build();
      
      AppenderRef ref = AppenderRef.createAppenderRef(APPENDER_NAME, null, null);
      AppenderRef[] refs = new AppenderRef[] {ref};
      
      final AsyncAppender asyncAppender = AsyncAppender.newBuilder().setAppenderRefs(refs).setName(APPENDER_NAME).setConfiguration(config).build();
      
      LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, LOGGER_NAME, LOGGER_NAME, refs, null, null, null);
      loggerConfig.addAppender(asyncAppender, null, null);
      
      
      config.addAppender(asyncAppender);
      config.addLogger(LOGGER_NAME, loggerConfig);
      
      
      context.updateLoggers(config);
      
      final Logger logger = LogManager.getContext().getLogger(LOGGER_NAME);
      
      
      logger.info(&quot;HELLO_WORLD&quot;);
      
  }
  </code></pre>