package io.vertx.core.logging;
import io.vertx.core.spi.logging.LogDelegate;
import io.vertx.core.spi.logging.LogDelegateFactory;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class LoggerFactory {
public static final String LOGGER_DELEGATE_FACTORY_CLASS_NAME = "vertx.logger-delegate-factory-class-name";
private static volatile LogDelegateFactory delegateFactory;
private static final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<>();
static {
initialise();
}
public static synchronized void initialise() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String className;
try {
className = System.getProperty(LOGGER_DELEGATE_FACTORY_CLASS_NAME);
} catch (Exception ignore) {
className = null;
}
if (className != null && configureWith(className, false, loader)) {
return;
}
if (loader.getResource("vertx-default-jul-logging.properties") == null) {
if (configureWith("SLF4J", true, loader)
|| configureWith("Log4j2", true, loader)) {
return;
}
}
delegateFactory = new JULLogDelegateFactory();
}
private static boolean configureWith(String name, boolean shortName, ClassLoader loader) {
String loggerName = LoggerFactory.class.getName();
try {
Class<?> clazz = Class.forName(shortName ? "io.vertx.core.logging." + name + "LogDelegateFactory" : name, true, loader);
LogDelegateFactory factory = (LogDelegateFactory) clazz.newInstance();
if (!factory.isAvailable()) {
return false;
}
factory.createDelegate(loggerName).debug("Using " + factory.getClass().getName());
delegateFactory = factory;
return true;
} catch (Throwable ignore) {
return false;
}
}
@Deprecated
public static Logger getLogger(final Class<?> clazz) {
String name = clazz.isAnonymousClass() ?
clazz.getEnclosingClass().getCanonicalName() :
clazz.getCanonicalName();
return getLogger(name);
}
@Deprecated
public static Logger getLogger(final String name) {
Logger logger = loggers.get(name);
if (logger == null) {
LogDelegate delegate = delegateFactory.createDelegate(name);
logger = new Logger(delegate);
Logger oldLogger = loggers.putIfAbsent(name, logger);
if (oldLogger != null) {
logger = oldLogger;
}
}
return logger;
}
@Deprecated
public static void removeLogger(String name) {
loggers.remove(name);
}
}