Class ExceptionLogging
- java.lang.Object
-
- com.machinezoo.noexception.slf4j.ExceptionLogging
-
public class ExceptionLogging extends Object
Static methods for creating exception handlers that log exceptions to SLF4J loggers. These handlers complement core handlers defined inExceptionsclass.Typical usage:
ExceptionLogging.log().get(() -> my_throwing_lambda).orElse(fallback)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ExceptionHandlerlog()ReturnsExceptionHandlerthat writes all exceptions to common logger.static ExceptionHandlerlog(org.slf4j.Logger logger)CreatesExceptionHandlerthat writes all exceptions to the specifiedlogger.static ExceptionHandlerlog(org.slf4j.Logger logger, String message)CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith the specifiedmessage.static ExceptionHandlerlog(org.slf4j.Logger logger, Supplier<String> message)CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith lazily evaluatedmessage.
-
-
-
Method Detail
-
log
public static ExceptionHandler log()
ReturnsExceptionHandlerthat 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. Uselog(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. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Returns:
- logging exception handler
- See Also:
-
log(Logger)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger)
CreatesExceptionHandlerthat writes all exceptions to the specifiedlogger. Most application code can use the more convenientlog()method. Uselog(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. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Parameters:
-
logger- where all exceptions are logged - Returns:
- exception handler with custom logger
- Throws:
-
NullPointerException- ifloggerisnull - See Also:
-
log(),log(Logger, String)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger, String message)
CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith the specifiedmessage. If you just need to specify custom logger, uselog(Logger). This overload allows for differentiating or explanatory message. If the message is expensive to construct, uselog(Logger, Supplier)method.Typical usage:
Exceptions.log(logger, "Failed to do X.").run(() -> my_throwing_lambda)No exceptions are allowed through, not even
Errors. IfInterruptedExceptionis 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- ifloggerormessageisnull - See Also:
-
log(Logger),log(Logger, Supplier)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger, Supplier<String> message)
CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith lazily evaluatedmessage. If the message does not need lazy evaluation, use thelog(Logger, String)method. This overload constructs expensive messages lazily only when exception is caught. Ifmessagethrows, 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. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Parameters:
-
logger- where all exceptions are logged -
message- aSupplierthat is lazily evaluated to generate log message - Returns:
- exception handler with custom logger and lazily evaluated message
- Throws:
-
NullPointerException- ifloggerormessageisnull - See Also:
-
log(Logger, String)
-
-