
com.github.schnitker.logmgr.JulLogManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logmgr-factory Show documentation
Show all versions of logmgr-factory Show documentation
JDK LogManager implementation for servlet containers
The newest version!
package com.github.schnitker.logmgr;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.WeakHashMap;
/**
* LogManager that uses {@link JulLoggerFactory} to find other logging systems via {@link ServiceLoader}.
*
* This LogManager is intended for Servlet containers. You must define the system
* property "java.util.logging.manager" to use this class.
* Every class loader has an own {@link JulLoggerFactory} to route JUL logging calls to other frameworks.
* If the factory is not configured yet, the manager calls the underlying system.
*
* The {@link JulLoggerFactory} implementations should use ServletContextListener to enable the routing.
*
+------------------+--------------------+
| Bootstrap |
| (logmgr-factory) |
+------------------+--------------------+
| Webapp1 | Webapp2 |
| (logmgr-log4j) | (logmgr-logback) |
+------------------+--------------------+
*
*/
public class JulLogManager extends java.util.logging.LogManager {
protected final Map classLoaderFactories = new WeakHashMap();
@Override
public java.util.logging.Logger getLogger(final String name) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
JulLoggerFactory factory = classLoaderFactories.get(cl);
if (factory == null) {
ServiceLoader loader = ServiceLoader.load(JulLoggerFactory.class, cl);
try {
Iterator iter = loader.iterator();
if (iter.hasNext()) {
factory = loader.iterator().next();
}
} catch (ServiceConfigurationError e) {
System.err.println("LogManager configuration error: " + e.getMessage());
}
// no implementation found
if (factory == null) {
factory = new JulLoggerFactoryNotFound();
}
classLoaderFactories.put(cl, factory);
}
if (!factory.isConfigured()) {
return super.getLogger(name);
}
return factory.getLogger(name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy