/** * */ package org.gecko.logging.log4j; import java.util.AbstractMap.SimpleEntry; import java.util.logging.Handler; import java.util.logging.LogManager; import org.osgi.annotation.bundle.Header; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogReaderService; import org.osgi.service.log.admin.LoggerAdmin; import org.osgi.util.tracker.ServiceTracker; /** * @author mark * */ @Header(name = Constants.BUNDLE_ACTIVATOR, value = "${@class}") public class Activator implements BundleActivator { static { removeHandlersForRootLogger(); install(); } private volatile ServiceTracker loggerAdminTracker; @Override public void start(BundleContext bundleContext) throws Exception { loggerAdminTracker = new ServiceTracker( bundleContext, LoggerAdmin.class, null) { @Override public LogReaderServiceTracker addingService( ServiceReference reference) { LoggerAdmin loggerAdmin = bundleContext.getService(reference); LogReaderServiceTracker lrst = new LogReaderServiceTracker(bundleContext, loggerAdmin); lrst.open(); return lrst; } @Override public void removedService( ServiceReference reference, LogReaderServiceTracker lrst) { lrst.close(); } }; loggerAdminTracker.open(); } @Override public void stop(BundleContext context) throws Exception { loggerAdminTracker.close(); uninstall(); } private static java.util.logging.Logger getRootLogger() { return LogManager.getLogManager().getLogger(""); } /** * Adds a Log4JJULBridgeHandler instance to jul's root logger. *

*

* This handler will redirect j.u.l. logging to Log4J. However, only logs enabled * in j.u.l. will be redirected. For example, if a log statement invoking a * j.u.l. logger is disabled, then the corresponding non-event will not * reach Log4JBridgeHandler and cannot be redirected. */ public static void install() { LogManager.getLogManager().getLogger("").addHandler(new Log4JBridgeHandler()); } /** * Removes previously installed Log4JJULBridgeHandler instances. * * @throws SecurityException A SecurityException is thrown, if a security manager * exists and if the caller does not have * LoggingPermission("control"). */ public static void uninstall() throws SecurityException { java.util.logging.Logger rootLogger = getRootLogger(); Handler[] handlers = rootLogger.getHandlers(); for (int i = 0; i < handlers.length; i++) { if (handlers[i] instanceof Log4JBridgeHandler) { rootLogger.removeHandler(handlers[i]); } } } /** * Invoking this method removes/unregisters/detaches all handlers currently attached to the root logger * @since 1.6.5 */ public static void removeHandlersForRootLogger() { java.util.logging.Logger rootLogger = getRootLogger(); java.util.logging.Handler[] handlers = rootLogger.getHandlers(); for (int i = 0; i < handlers.length; i++) { rootLogger.removeHandler(handlers[i]); } } class LogReaderServiceTracker extends ServiceTracker { public LogReaderServiceTracker(BundleContext context, LoggerAdmin loggerAdmin) { super(context, LogReaderService.class, null); this.loggerAdmin = loggerAdmin; } @Override public Pair addingService( ServiceReference reference) { LogReaderService logReaderService = context.getService(reference); Log4JLogListener log4jLogListener = new Log4JLogListener(loggerAdmin); logReaderService.addLogListener(log4jLogListener); return new Pair(logReaderService, log4jLogListener); } @Override public void removedService( ServiceReference reference, Pair pair) { pair.getKey().removeLogListener(pair.getValue()); } private final LoggerAdmin loggerAdmin; } class Pair extends SimpleEntry { private static final long serialVersionUID = 1L; public Pair(LogReaderService logReaderService, Log4JLogListener log4JLogListener) { super(logReaderService, log4JLogListener); } } }