title: Dynamically/Programmatically configuring your own asynclogger in Log4j2
body: <p>I have an old log4j1.x already implemented in my project where we have created our own logger by extending the the class <i>org.apach.log4j.logger</i> .But now we want to move to Log4j2 as it has asynLogger.Is the below code fulfills all the features of async logging supported my log4j2 when created programmatically</p>
  
  <p><i></p>
  
  <pre><code>        import java.util.HashMap;
  
          import org.apache.logging.log4j.Level;
          import org.apache.logging.log4j.core.LoggerContext;
          import org.apache.logging.log4j.core.appender.ConsoleAppender;
          import org.apache.logging.log4j.core.appender.RandomAccessFileAppender;
          import org.apache.logging.log4j.core.async.AsyncLoggerContext;
          import org.apache.logging.log4j.core.layout.PatternLayout;
          import org.apache.logging.log4j.message.FormattedMessageFactory;
          import org.apache.logging.log4j.message.MessageFactory;
  
          public final class MyLog4j2Logger extends
                  org.apache.logging.log4j.core.async.AsyncLogger {
  
              private static HashMap&lt;String, MyLog4j2Logger&gt; logCache = new HashMap&lt;String, MyLog4j2Logger&gt;(4);
  
              private MyLog4j2Logger(LoggerContext context, String name,MessageFactory msgFactory) {
                  super(context, name, msgFactory);
                  System.out.println("Using AsyncLogger");
  
                  // Set to all levels
                  this.setLevel(Level.ALL);
              }
  
              /*
              * Factory Method that returns an instance of Logger. returns if the
              * instance is already present in the Hash Table else creates now and
              * returns it.
              */
              public final static MyLog4j2Logger getCustomLogger(String sCategory) {
                  try {
                      System.setProperty("Log4jContextSelector",
                              "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
                      // Check if Instance is already present for the Category
                      if (logCache.containsKey(sCategory)) {
                          MyLog4j2Logger logger = logCache.get(sCategory);
                          logger.info("Getting already created logger for " + sCategory);
                          return logger;
                      } else {
  
                          // Create a new custom logger for this category
                          MyLog4j2Logger customLogger = configureCategoryLogger(sCategory);
  
                          // Add the custom logger to Table.
                          logCache.put(sCategory, customLogger);
  
                          customLogger.info("Created a New Logger for " + sCategory);
  
                          return (MyLog4j2Logger) customLogger;
                      }
                  } catch (Exception exp) {
                      System.out
                              .println("Exception thrown while fetching Logger instance");
                      exp.printStackTrace();
                  }
  
                  return null;
  
              }
  
              private static MyLog4j2Logger configureCategoryLogger(String sCategory) {
  
                  // Create logger context
                  LoggerContext context = new AsyncLoggerContext(sCategory);
  
                  // Create formatted message factory
                  MessageFactory msgFactory = new FormattedMessageFactory();
  
                  MyLog4j2Logger logger = new MyLog4j2Logger(context, sCategory,
                          msgFactory);
  
                  RandomAccessFileAppender appender = RandomAccessFileAppender
                          .createAppender(
                                  "C:\\temp\\App.log",// filename
                                  "true",// append
                                  "file_appender-" + sCategory,// name
                                  "true",// immediateFlush
                                  "",// bufferSize
                                  "true",// ignoreExceptions
                                  PatternLayout.createLayout(
                                          "%-5p - [%d] - [%t] - [%l]  : %m%n", null,
                                          null, "UTF-8", "true", sCategory), null,// filter
                                  "false",// advertise
                                  null,// advertiseURI
                                  null// config
                          );
  
                  ConsoleAppender consoleAppender = ConsoleAppender.createAppender(
                          PatternLayout.createLayout("%-5p - [%d] - [%t] - [%l]  : %m%n",
                                  null, null, "UTF-8", null, null), null, null,
                          "Console", null, null);
  
                  appender.start();
                  consoleAppender.stop();
                  logger.getContext().getConfiguration().getLoggerConfig(sCategory)
                          .addAppender(appender, Level.TRACE, null);
                  logger.getContext().getConfiguration().getLoggerConfig(sCategory)
                          .addAppender(consoleAppender, Level.OFF, null);
                  System.out.println(logger.getContext().getConfiguration()
                          .getAppenders());
  
                  return logger;
  
              }
  
              public static void main(String[] args) {
  
                  MyLog4j2Logger logger = MyLog4j2Logger.getCustomLogger("TESTING");
                  logger.debug("Debug MSG");
                  logger.error("Error MSG");
                  logger.info("INFO msg");
  
              }
  
          }
  </code></pre>
  
  <p></i></p>