Class ExceptionLogging


  • public class ExceptionLogging
    extends Object
    Static methods for creating exception handlers that log exceptions to SLF4J loggers. These handlers complement core handlers defined in Exceptions class.

    Typical usage: ExceptionLogging.log().get(() -> my_throwing_lambda).orElse(fallback)

    See Also:
    Homepage of SLF4J extension for NoException
    • Method Detail

      • log

        public static ExceptionHandler log()
        Returns ExceptionHandler that writes all exceptions to common logger. Logs are written to SLF4J logger named after this class. This handler is convenient and a suitable default choice, but the single shared logger can make logs harder to filter. Use log(Logger) to specify custom logger where filtering is important.

        Typical usage: Exceptions.log().run(() -> my_throwing_lambda)

        No exceptions are allowed through, not even Errors. If InterruptedException is caught, Thread.interrupt() is called.

        Returns:
        logging exception handler
        See Also:
        log(Logger)
      • log

        public static ExceptionHandler log​(org.slf4j.Logger logger)
        Creates ExceptionHandler that writes all exceptions to the specified logger. Most application code can use the more convenient log() method. Use log(Logger, String) overload to specify unique message where necessary.

        Typical usage: Exceptions.log(logger).run(() -> my_throwing_lambda)

        No exceptions are allowed through, not even Errors. If InterruptedException is caught, Thread.interrupt() is called.

        Parameters:
        logger - where all exceptions are logged
        Returns:
        exception handler with custom logger
        Throws:
        NullPointerException - if logger is null
        See Also:
        log(), log(Logger, String)
      • log

        public static ExceptionHandler log​(org.slf4j.Logger logger,
                                           String message)
        Creates ExceptionHandler that writes all exceptions to the specified logger with the specified message. If you just need to specify custom logger, use log(Logger). This overload allows for differentiating or explanatory message. If the message is expensive to construct, use log(Logger, Supplier) method.

        Typical usage: Exceptions.log(logger, "Failed to do X.").run(() -> my_throwing_lambda)

        No exceptions are allowed through, not even Errors. If InterruptedException is caught, Thread.interrupt() is called.

        Parameters:
        logger - where all exceptions are logged
        message - introduces every exception in the log
        Returns:
        exception handler with custom logger and message
        Throws:
        NullPointerException - if logger or message is null
        See Also:
        log(Logger), log(Logger, Supplier)
      • log

        public static ExceptionHandler log​(org.slf4j.Logger logger,
                                           Supplier<String> message)
        Creates ExceptionHandler that writes all exceptions to the specified logger with lazily evaluated message. If the message does not need lazy evaluation, use the log(Logger, String) method. This overload constructs expensive messages lazily only when exception is caught. If message throws, the exception is logged and fallback message is used to log the original exception.

        Typical usage: Exceptions.log(logger, () -> "Exception in " + this).run(() -> my_throwing_lambda)

        No exceptions are allowed through, not even Errors. If InterruptedException is caught, Thread.interrupt() is called.

        Parameters:
        logger - where all exceptions are logged
        message - a Supplier that is lazily evaluated to generate log message
        Returns:
        exception handler with custom logger and lazily evaluated message
        Throws:
        NullPointerException - if logger or message is null
        See Also:
        log(Logger, String)