Last active
August 29, 2015 14:07
-
-
Save xiaodong-xie/219491e0b433f8bd451e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.slf4j.bridge; | |
import java.util.logging.Handler; | |
import java.util.logging.Level; | |
import java.util.logging.LogManager; | |
import java.util.logging.LogRecord; | |
import org.jboss.logmanager.ExtHandler; | |
import org.jboss.logmanager.ExtLogRecord; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.slf4j.spi.LocationAwareLogger; | |
public class SLF4JBridgeHandler extends ExtHandler { | |
// The caller is java.util.logging.Logger | |
private static final String FQCN = java.util.logging.Logger.class.getName(); | |
private static final String UNKNOWN_LOGGER_NAME = "unknown.jul.logger"; | |
private static final int TRACE_LEVEL_THRESHOLD = Level.FINEST.intValue(); | |
private static final int DEBUG_LEVEL_THRESHOLD = Level.FINE.intValue(); | |
private static final int INFO_LEVEL_THRESHOLD = Level.INFO.intValue(); | |
private static final int WARN_LEVEL_THRESHOLD = Level.WARNING.intValue(); | |
/** | |
* Adds a SLF4JBridgeHandler instance to jul's root logger. | |
* <p/> | |
* <p/> | |
* This handler will redirect j.u.l. logging to SLF4J. 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 <em>not</em> | |
* reach SLF4JBridgeHandler and cannot be redirected. | |
*/ | |
public static void install() { | |
LogManager.getLogManager().getLogger("").addHandler( | |
new SLF4JBridgeHandler() | |
); | |
} | |
private static java.util.logging.Logger getRootLogger() { | |
return LogManager.getLogManager().getLogger(""); | |
} | |
/** | |
* Removes previously installed SLF4JBridgeHandler instances. See also | |
* {@link #install()}. | |
* | |
* @throws SecurityException A <code>SecurityException</code> 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 SLF4JBridgeHandler) { | |
rootLogger.removeHandler(handlers[i]); | |
} | |
} | |
} | |
/** | |
* Returns true if SLF4JBridgeHandler has been previously installed, returns false otherwise. | |
* | |
* @return true if SLF4JBridgeHandler is already installed, false other wise | |
* @throws SecurityException | |
*/ | |
public static boolean isInstalled() throws SecurityException { | |
java.util.logging.Logger rootLogger = getRootLogger(); | |
Handler[] handlers = rootLogger.getHandlers(); | |
for (int i = 0; i < handlers.length; i++) { | |
if (handlers[i] instanceof SLF4JBridgeHandler) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* 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]); | |
} | |
} | |
/** | |
* Initialize this handler. | |
*/ | |
public SLF4JBridgeHandler() { | |
} | |
/** | |
* Return the Logger instance that will be used for logging. | |
*/ | |
protected Logger getSLF4JLogger(LogRecord record) { | |
String name = record.getLoggerName(); | |
if (name == null) { | |
name = UNKNOWN_LOGGER_NAME; | |
} | |
return LoggerFactory.getLogger(name); | |
} | |
protected void callLocationAwareLogger( | |
LocationAwareLogger lal, | |
ExtLogRecord record | |
) { | |
int julLevelValue = record.getLevel().intValue(); | |
int slf4jLevel; | |
if (julLevelValue <= TRACE_LEVEL_THRESHOLD) { | |
slf4jLevel = LocationAwareLogger.TRACE_INT; | |
} else if (julLevelValue <= DEBUG_LEVEL_THRESHOLD) { | |
slf4jLevel = LocationAwareLogger.DEBUG_INT; | |
} else if (julLevelValue <= INFO_LEVEL_THRESHOLD) { | |
slf4jLevel = LocationAwareLogger.INFO_INT; | |
} else if (julLevelValue <= WARN_LEVEL_THRESHOLD) { | |
slf4jLevel = LocationAwareLogger.WARN_INT; | |
} else { | |
slf4jLevel = LocationAwareLogger.ERROR_INT; | |
} | |
String i18nMessage = record.getFormattedMessage(); | |
lal.log(null, FQCN, slf4jLevel, i18nMessage, null, record.getThrown()); | |
} | |
protected void callPlainSLF4JLogger(Logger slf4jLogger, ExtLogRecord record) { | |
String i18nMessage = record.getFormattedMessage(); | |
int julLevelValue = record.getLevel().intValue(); | |
if (julLevelValue <= TRACE_LEVEL_THRESHOLD) { | |
slf4jLogger.trace(i18nMessage, record.getThrown()); | |
} else if (julLevelValue <= DEBUG_LEVEL_THRESHOLD) { | |
slf4jLogger.debug(i18nMessage, record.getThrown()); | |
} else if (julLevelValue <= INFO_LEVEL_THRESHOLD) { | |
slf4jLogger.info(i18nMessage, record.getThrown()); | |
} else if (julLevelValue <= WARN_LEVEL_THRESHOLD) { | |
slf4jLogger.warn(i18nMessage, record.getThrown()); | |
} else { | |
slf4jLogger.error(i18nMessage, record.getThrown()); | |
} | |
} | |
@Override | |
protected void doPublish(ExtLogRecord record) { | |
// Silently ignore null records. | |
if (record == null) { | |
return; | |
} | |
Map<String, String> mdc = record.getMdcCopy(); | |
if (mdc != null) { | |
MDC.setContextMap(mdc); | |
} | |
Logger slf4jLogger = getSLF4JLogger(record); | |
if (slf4jLogger instanceof LocationAwareLogger) { | |
callLocationAwareLogger((LocationAwareLogger) slf4jLogger, record); | |
} else { | |
callPlainSLF4JLogger(slf4jLogger, record); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment