package org.gecko.logging.log4j; import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.LocalizedMessage; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.spi.LocationAwareLogger; public class Log4JBridgeHandler extends Handler { // 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(); /** * Publish a LogRecord. *
* The logging request was made initially to a Logger object, which * initialized the LogRecord and forwarded it here. * * This handler ignores the Level attached to the LogRecord, as Log4J cares * about discarding log statements. * * @param record Description of the log event. A null record is silently ignored * and is not published. */ @Override public void publish(LogRecord record) { // Silently ignore null records. if (record == null) { return; } Logger log4jLogger = getLog4JLogger(record); String message = record.getMessage(); // can be null! // this is a check to avoid calling the underlying logging system // with a null message. While it is legitimate to invoke j.u.l. with // a null message, other logging frameworks do not support this. // see also http://jira.qos.ch/browse/SLF4J-99 if (message == null) { message = ""; } if (log4jLogger instanceof LocationAwareLogger) { callLocationAwareLogger((LocationAwareLogger) log4jLogger, record); } else { callPlainLog4JLogger(log4jLogger, record); } } @Override public void flush() { // empty } @Override public void close() throws SecurityException { // empty } /** * Return the Logger instance that will be used for logging. */ protected Logger getLog4JLogger(LogRecord record) { String name = record.getLoggerName(); if (name == null) { name = UNKNOWN_LOGGER_NAME; } return org.apache.logging.log4j.LogManager.getLogger(name); } protected void callLocationAwareLogger(LocationAwareLogger lal, LogRecord record) { int julLevelValue = record.getLevel().intValue(); org.apache.logging.log4j.Level log4jLevel; if (julLevelValue <= TRACE_LEVEL_THRESHOLD) { log4jLevel = org.apache.logging.log4j.Level.TRACE; } else if (julLevelValue <= DEBUG_LEVEL_THRESHOLD) { log4jLevel = org.apache.logging.log4j.Level.DEBUG; } else if (julLevelValue <= INFO_LEVEL_THRESHOLD) { log4jLevel = org.apache.logging.log4j.Level.INFO; } else if (julLevelValue <= WARN_LEVEL_THRESHOLD) { log4jLevel = org.apache.logging.log4j.Level.WARN; } else { log4jLevel = org.apache.logging.log4j.Level.ERROR; } Message i18nMessage = getMessageI18N(record); lal.logMessage(log4jLevel, null, FQCN, null, i18nMessage, record.getThrown()); } protected void callPlainLog4JLogger(Logger slf4jLogger, LogRecord record) { String i18nMessage = getMessageStringI18N(record); 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()); } } /** * Get the record's message, possibly via a resource bundle. * * @param record * @return */ private String getMessageStringI18N(LogRecord record) { String message = record.getMessage(); if (message == null) { return null; } ResourceBundle bundle = record.getResourceBundle(); if (bundle != null) { try { message = bundle.getString(message); } catch (MissingResourceException e) { } } Object[] params = record.getParameters(); // avoid formatting when there are no or 0 parameters. see also // http://jira.qos.ch/browse/SLF4J-203 if (params != null && params.length > 0) { try { message = MessageFormat.format(message, params); } catch (IllegalArgumentException e) { // default to the same behavior as in java.util.logging.Formatter.formatMessage(LogRecord) // see also http://jira.qos.ch/browse/SLF4J-337 return message; } } return message; } /** * Get the record's message, possibly via a resource bundle. * * @param record * @return */ private Message getMessageI18N(LogRecord record) { String message = record.getMessage(); if (message == null) { return null; } ResourceBundle bundle = record.getResourceBundle(); Object[] params = record.getParameters(); Message m = null; // avoid formatting when there are no or 0 parameters. see also // http://jira.qos.ch/browse/SLF4J-203 if (params != null && params.length > 0) { m = new LocalizedMessage(bundle, message, params); } else { m = new LocalizedMessage(bundle, message); } return m; } }