package org.apache.logging.log4j;

import java.net.URI;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.message.StringFormatterMessageFactory;
import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
import org.apache.logging.log4j.spi.LoggerContext;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.apache.logging.log4j.spi.Provider;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.LoaderUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.ProviderUtil;

public class LogManager {
   private static volatile LoggerContextFactory factory;
   public static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory";
   private static final Logger LOGGER = StatusLogger.getLogger();
   public static final String ROOT_LOGGER_NAME = "";

   public static boolean exists(String name) {
      return getContext().hasLogger(name);
   }

   private static String getClassName(int depth) {
      return (new Throwable()).getStackTrace()[depth].getClassName();
   }

   public static LoggerContext getContext() {
      return factory.getContext(LogManager.class.getName(), (ClassLoader)null, (Object)null, true);
   }

   public static LoggerContext getContext(boolean currentContext) {
      return factory.getContext(LogManager.class.getName(), (ClassLoader)null, (Object)null, currentContext, (URI)null, (String)null);
   }

   public static LoggerContext getContext(ClassLoader loader, boolean currentContext) {
      return factory.getContext(LogManager.class.getName(), loader, (Object)null, currentContext);
   }

   public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext) {
      return factory.getContext(LogManager.class.getName(), loader, externalContext, currentContext);
   }

   public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation) {
      return factory.getContext(LogManager.class.getName(), loader, (Object)null, currentContext, configLocation, (String)null);
   }

   public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext, URI configLocation) {
      return factory.getContext(LogManager.class.getName(), loader, externalContext, currentContext, configLocation, (String)null);
   }

   public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext, URI configLocation, String name) {
      return factory.getContext(LogManager.class.getName(), loader, externalContext, currentContext, configLocation, name);
   }

   protected static LoggerContext getContext(String fqcn, boolean currentContext) {
      return factory.getContext(fqcn, (ClassLoader)null, (Object)null, currentContext);
   }

   protected static LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
      return factory.getContext(fqcn, loader, (Object)null, currentContext);
   }

   public static LoggerContextFactory getFactory() {
      return factory;
   }

   public static void setFactory(LoggerContextFactory factory) {
      LogManager.factory = factory;
   }

   public static Logger getFormatterLogger(Class<?> clazz) {
      return getLogger((String)(clazz != null ? clazz.getName() : getClassName(2)), (MessageFactory)StringFormatterMessageFactory.INSTANCE);
   }

   public static Logger getFormatterLogger(Object value) {
      return getLogger((String)(value != null ? value.getClass().getName() : getClassName(2)), (MessageFactory)StringFormatterMessageFactory.INSTANCE);
   }

   public static Logger getFormatterLogger(String name) {
      return getLogger((String)(name != null ? name : getClassName(2)), (MessageFactory)StringFormatterMessageFactory.INSTANCE);
   }

   public static Logger getLogger() {
      return getLogger(getClassName(2));
   }

   public static Logger getLogger(Class<?> clazz) {
      return getLogger(clazz != null ? clazz.getName() : getClassName(2));
   }

   public static Logger getLogger(Class<?> clazz, MessageFactory messageFactory) {
      return getLogger(clazz != null ? clazz.getName() : getClassName(2), messageFactory);
   }

   public static Logger getLogger(MessageFactory messageFactory) {
      return getLogger(getClassName(2), messageFactory);
   }

   public static Logger getLogger(Object value) {
      return getLogger(value != null ? value.getClass().getName() : getClassName(2));
   }

   public static Logger getLogger(Object value, MessageFactory messageFactory) {
      return getLogger(value != null ? value.getClass().getName() : getClassName(2), messageFactory);
   }

   public static Logger getLogger(String name) {
      String actualName = name != null ? name : getClassName(2);
      return factory.getContext(LogManager.class.getName(), (ClassLoader)null, (Object)null, false).getLogger(actualName);
   }

   public static Logger getLogger(String name, MessageFactory messageFactory) {
      String actualName = name != null ? name : getClassName(2);
      return factory.getContext(LogManager.class.getName(), (ClassLoader)null, (Object)null, false).getLogger(actualName, messageFactory);
   }

   protected static Logger getLogger(String fqcn, String name) {
      return factory.getContext(fqcn, (ClassLoader)null, (Object)null, false).getLogger(name);
   }

   public static Logger getRootLogger() {
      return getLogger("");
   }

   protected LogManager() {
   }

   static {
      PropertiesUtil managerProps = PropertiesUtil.getProperties();
      String factoryClassName = managerProps.getStringProperty("log4j2.loggerContextFactory");
      ClassLoader cl = LoaderUtil.getThreadContextClassLoader();
      if (factoryClassName != null) {
         try {
            Class<?> clazz = cl.loadClass(factoryClassName);
            if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
               factory = (LoggerContextFactory)clazz.newInstance();
            }
         } catch (ClassNotFoundException var9) {
            LOGGER.error("Unable to locate configured LoggerContextFactory {}", factoryClassName);
         } catch (Exception var10) {
            LOGGER.error("Unable to create configured LoggerContextFactory {}", factoryClassName, var10);
         }
      }

      if (factory == null) {
         SortedMap<Integer, LoggerContextFactory> factories = new TreeMap();
         if (ProviderUtil.hasProviders()) {
            Iterator i$ = ProviderUtil.getProviders().iterator();

            while(i$.hasNext()) {
               Provider provider = (Provider)i$.next();
               Class<? extends LoggerContextFactory> factoryClass = provider.loadLoggerContextFactory();
               if (factoryClass != null) {
                  try {
                     factories.put(provider.getPriority(), factoryClass.newInstance());
                  } catch (Exception var8) {
                     LOGGER.error("Unable to create class {} specified in {}", factoryClass.getName(), provider.getUrl().toString(), var8);
                  }
               }
            }

            if (factories.isEmpty()) {
               LOGGER.error("Unable to locate a logging implementation, using SimpleLogger");
               factory = new SimpleLoggerContextFactory();
            } else {
               StringBuilder sb = new StringBuilder("Multiple logging implementations found: \n");
               Iterator i$ = factories.entrySet().iterator();

               while(i$.hasNext()) {
                  Entry<Integer, LoggerContextFactory> entry = (Entry)i$.next();
                  sb.append("Factory: ").append(((LoggerContextFactory)entry.getValue()).getClass().getName());
                  sb.append(", Weighting: ").append(entry.getKey()).append('\n');
               }

               factory = (LoggerContextFactory)factories.get(factories.lastKey());
               sb.append("Using factory: ").append(factory.getClass().getName());
               LOGGER.warn(sb.toString());
            }
         } else {
            LOGGER.error("Unable to locate a logging implementation, using SimpleLogger");
            factory = new SimpleLoggerContextFactory();
         }
      }

   }
}
