Class Log4jBridgeHandler

  • All Implemented Interfaces:
    java.beans.PropertyChangeListener, java.util.EventListener

    public class Log4jBridgeHandler
    extends java.util.logging.Handler
    implements java.beans.PropertyChangeListener
    Bridge from JUL to log4j2.
    This is an alternative to log4j.jul.LogManager (running as complete JUL replacement), especially useful for webapps running on a container for which the LogManager cannot or should not be used.

    Installation/usage:
    • Declaratively inside JUL's logging.properties:
      handlers = org.apache.logging.log4j.jul.Log4jBridgeHandler
      (and typically also:   org.apache.logging.log4j.jul.Log4jBridgeHandler.propagateLevels = true )
      Note: in a webapp running on Tomcat, you may create a WEB-INF/classes/logging.properties file to configure JUL for this webapp only: configured handlers and log levels affect your webapp only! This file is then the complete JUL configuration, so JUL's defaults (e.g. log level INFO) apply for stuff not explicitly defined therein.
    • Programmatically by calling install() method, e.g. inside ServletContextListener static-class-init. or contextInitialized()
    Configuration (in JUL's logging.properties):
    • Log4jBridgeHandler.suffixToAppend
      String, suffix to append to JUL logger names, to easily recognize bridged log messages. A dot "." is automatically prepended, so configuration for the basis logger is used
      Example: Log4jBridgeHandler.suffixToAppend = _JUL
      Useful, for example, if you use JSF because it logs exceptions and throws them afterwards; you can easily recognize the duplicates with this (or concentrate on the non-JUL-logs).
    • Log4jBridgeHandler.propagateLevels   boolean, "true" to automatically propagate log4j log levels to JUL.
    • Log4jBridgeHandler.sysoutDebug   boolean, perform some (developer) debug output to sysout
    Log levels are translated with LevelTranslator, see also log4j doc.

    Restrictions:
    • Manually given source/location info in JUL (e.g. entering(), exiting(), throwing(), logp(), logrb() ) will NOT be considered, i.e. gets lost in log4j logging.
    • Log levels of JUL have to be adjusted according to log4j log levels: Either by using "propagateLevels" (preferred), or manually by specifying them explicitly, i.e. logging.properties and log4j2.xml have some redundancies.
    • Only JUL log events that are allowed according to the JUL log level get to this handler and thus to log4j. This is only relevant and important if you NOT use "propagateLevels". If you set .level = SEVERE only error logs will be seen by this handler and thus log4j - even if the corresponding log4j log level is ALL.
      On the other side, you should NOT set .level = FINER or FINEST if the log4j level is higher. In this case a lot of JUL log events would be generated, sent via this bridge to log4j and thrown away by the latter.
      Note: JUL's default log level (i.e. none specified in logger.properties) is INFO.
    (Credits: idea and concept originate from org.slf4j.bridge.SLF4JBridgeHandler; level propagation idea originates from logback/LevelChangePropagator; but no source code has been copied)
    • Constructor Summary

      Constructors 
      Constructor Description
      Log4jBridgeHandler()
      Initialize this handler by reading out JUL configuration.
      Log4jBridgeHandler​(boolean debugOutput, java.lang.String suffixToAppend, boolean propagateLevels)
      Initialize this handler with given configuration.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      private static java.util.logging.Logger getJulRootLogger()  
      private Logger getLog4jLogger​(java.util.logging.LogRecord record)
      Return the log4j-Logger instance that will be used for logging.
      protected void init​(boolean debugOutput, java.lang.String suffixToAppend, boolean propagateLevels)
      Perform init.
      static void install​(boolean removeHandlersForRootLogger, java.lang.String suffixToAppend, boolean propagateLevels)
      Adds a new Log4jBridgeHandler instance to JUL's root logger.
      private void propagateLogLevels​(Configuration config)
      Perform developer tests? (Should be unused/outcommented for real code)
      void propertyChange​(java.beans.PropertyChangeEvent evt)  
      void publish​(java.util.logging.LogRecord record)  
      • Methods inherited from class java.util.logging.Handler

        getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SLOGGER

        private static final Logger SLOGGER
      • FQCN

        private static final java.lang.String FQCN
      • UNKNOWN_LOGGER_NAME

        private static final java.lang.String UNKNOWN_LOGGER_NAME
        See Also:
        Constant Field Values
      • julFormatter

        private static final java.util.logging.Formatter julFormatter
      • doDebugOutput

        private boolean doDebugOutput
      • julSuffixToAppend

        private java.lang.String julSuffixToAppend
      • julLoggerRefs

        private java.util.Set<java.util.logging.Logger> julLoggerRefs
        Save "hard" references to configured JUL loggers. (is lazy init.)
    • Constructor Detail

      • Log4jBridgeHandler

        public Log4jBridgeHandler()
        Initialize this handler by reading out JUL configuration.
      • Log4jBridgeHandler

        public Log4jBridgeHandler​(boolean debugOutput,
                                  java.lang.String suffixToAppend,
                                  boolean propagateLevels)
        Initialize this handler with given configuration.
    • Method Detail

      • install

        public static void install​(boolean removeHandlersForRootLogger,
                                   java.lang.String suffixToAppend,
                                   boolean propagateLevels)
        Adds a new Log4jBridgeHandler instance to JUL's root logger. This is a programmatic alternative to specify handlers = org.apache.logging.log4j.jul.Log4jBridgeHandler and its configuration in logging.properties.
        Parameters:
        removeHandlersForRootLogger - true to remove all other installed handlers on JUL root level
      • getJulRootLogger

        private static java.util.logging.Logger getJulRootLogger()
      • init

        protected void init​(boolean debugOutput,
                            java.lang.String suffixToAppend,
                            boolean propagateLevels)
        Perform init. of this handler with given configuration (typical use is for constructor).
      • close

        public void close()
        Specified by:
        close in class java.util.logging.Handler
      • publish

        public void publish​(java.util.logging.LogRecord record)
        Specified by:
        publish in class java.util.logging.Handler
      • flush

        public void flush()
        Specified by:
        flush in class java.util.logging.Handler
      • getLog4jLogger

        private Logger getLog4jLogger​(java.util.logging.LogRecord record)
        Return the log4j-Logger instance that will be used for logging. Handles null name case and appends configured suffix.
      • propertyChange

        public void propertyChange​(java.beans.PropertyChangeEvent evt)
        Specified by:
        propertyChange in interface java.beans.PropertyChangeListener
      • propagateLogLevels

        private void propagateLogLevels​(Configuration config)
        Perform developer tests? (Should be unused/outcommented for real code)