package com.alibaba.druid.support.logging;

import java.lang.reflect.Constructor;

public class LogFactory {
   private static Constructor logConstructor;

   private static void tryImplementation(String testClassName, String implClassName) {
      if (logConstructor == null) {
         try {
            Resources.classForName(testClassName);
            Class implClass = Resources.classForName(implClassName);
            logConstructor = implClass.getConstructor(String.class);
            Class<?> declareClass = logConstructor.getDeclaringClass();
            if (!Log.class.isAssignableFrom(declareClass)) {
               logConstructor = null;
            }

            try {
               if (null != logConstructor) {
                  logConstructor.newInstance(LogFactory.class.getName());
               }
            } catch (Throwable var5) {
               logConstructor = null;
            }
         } catch (Throwable var6) {
         }

      }
   }

   public static Log getLog(Class clazz) {
      return getLog(clazz.getName());
   }

   public static Log getLog(String loggerName) {
      try {
         return (Log)logConstructor.newInstance(loggerName);
      } catch (Throwable t) {
         throw new RuntimeException("Error creating logger for logger '" + loggerName + "'.  Cause: " + t, t);
      }
   }

   public static synchronized void selectLog4JLogging() {
      try {
         Resources.classForName("org.apache.log4j.Logger");
         Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
         logConstructor = implClass.getConstructor(String.class);
      } catch (Throwable var1) {
      }

   }

   public static synchronized void selectJavaLogging() {
      try {
         Resources.classForName("java.util.logging.Logger");
         Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
         logConstructor = implClass.getConstructor(String.class);
      } catch (Throwable var1) {
      }

   }

   static {
      String logType = System.getProperty("druid.logType");
      if (logType != null) {
         if (logType.equalsIgnoreCase("slf4j")) {
            tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
         } else if (logType.equalsIgnoreCase("log4j")) {
            tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");
         } else if (logType.equalsIgnoreCase("log4j2")) {
            tryImplementation("org.apache.logging.log4j.Logger", "com.alibaba.druid.support.logging.Log4j2Impl");
         } else if (logType.equalsIgnoreCase("commonsLog")) {
            tryImplementation("org.apache.commons.logging.LogFactory", "com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
         } else if (logType.equalsIgnoreCase("jdkLog")) {
            tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");
         }
      }

      tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
      tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");
      tryImplementation("org.apache.logging.log4j.Logger", "com.alibaba.druid.support.logging.Log4j2Impl");
      tryImplementation("org.apache.commons.logging.LogFactory", "com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
      tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");
      if (logConstructor == null) {
         try {
            logConstructor = NoLoggingImpl.class.getConstructor(String.class);
         } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
         }
      }

   }
}
