Package reactor.util

Class Loggers

java.lang.Object
reactor.util.Loggers

public abstract class Loggers extends Object
Expose static methods to get a logger depending on the environment. If SL4J is on the classpath, it will be used. Otherwise, there are two possible fallbacks: Console or java.util.logging.Logger). By default, the Console fallback is used. To use the JDK loggers, set the "reactor.logging.fallback" System property to "JDK".

One can also force the implementation by using the "useXXX" static methods: useConsoleLoggers(), useVerboseConsoleLoggers(), useJdkLoggers() and useSl4jLoggers() (which may throw an Exception if the library isn't on the classpath). Note that the system property method above is preferred, as no cleanup of the logger factory initialized at startup is attempted by the useXXX methods.

  • Field Details

    • FALLBACK_PROPERTY

      public static final String FALLBACK_PROPERTY
      The system property that determines which fallback implementation to use for loggers when SLF4J isn't available. Use JDK for the JDK-backed logging and anything else for Console-based (the default).
      See Also:
  • Method Details

    • resetLoggerFactory

      public static void resetLoggerFactory()
      Attempt to activate the best reactor Logger factory, by first attempting to use the SLF4J one, then falling back to either Console logging or java.util.logging.Logger). By default, the Console fallback is used. To fallback to the JDK loggers, set the "reactor.logging.fallback" System property to "JDK".
      See Also:
    • useConsoleLoggers

      public static void useConsoleLoggers()
      Force the usage of Console-based Loggers, even if SLF4J is available on the classpath. Console loggers will output ERROR and WARN levels to System.err and levels below to System.out. All levels except TRACE and DEBUG are considered enabled. TRACE and DEBUG are omitted.

      The previously active logger factory is simply replaced without any particular clean-up.

    • useVerboseConsoleLoggers

      public static void useVerboseConsoleLoggers()
      Force the usage of Console-based Loggers, even if SLF4J is available on the classpath. Console loggers will output ERROR and WARN levels to System.err and levels below to System.out. All levels (including TRACE and DEBUG) are considered enabled.

      The previously active logger factory is simply replaced without any particular clean-up.

    • useCustomLoggers

      public static void useCustomLoggers(Function<String,? extends Logger> loggerFactory)
      Use a custom type of Logger created through the provided Function, which takes a logger name as input.

      The previously active logger factory is simply replaced without any particular clean-up.

      Thread-safety

      Given logger acquisition function must be thread-safe. It means that it is user responsibility to ensure that any internal state and cache used by the provided function is properly synchronized.
      Parameters:
      loggerFactory - the Function that provides a (possibly cached) Logger given a name.
    • useJdkLoggers

      public static void useJdkLoggers()
      Force the usage of JDK-based Loggers, even if SLF4J is available on the classpath.

      The previously active logger factory is simply replaced without any particular clean-up.

    • useSl4jLoggers

      public static void useSl4jLoggers()
      Force the usage of SL4J-based Loggers, throwing an exception if SLF4J isn't available on the classpath. Prefer using resetLoggerFactory() as it will fallback in the later case.

      The previously active logger factory is simply replaced without any particular clean-up.

    • getLogger

      public static Logger getLogger(String name)
      Get a Logger.

      For a notion of how the backing implementation is chosen, see resetLoggerFactory() (or call one of the useXxxLoggers methods).

      Parameters:
      name - the category or logger name to use
      Returns:
      a new Logger instance
    • getLogger

      public static Logger getLogger(Class<?> cls)
      Get a Logger, backed by SLF4J if present on the classpath or falling back to java.util.logging.Logger.
      Parameters:
      cls - the source Class to derive the logger name from.
      Returns:
      a new Logger instance