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 inExceptions
class.Typical usage:
ExceptionLogging.log().get(() -> my_throwing_lambda).orElse(fallback)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ExceptionHandler
log()
ReturnsExceptionHandler
that writes all exceptions to common logger.static ExceptionHandler
log(org.slf4j.Logger logger)
CreatesExceptionHandler
that writes all exceptions to the specifiedlogger
.static ExceptionHandler
log(org.slf4j.Logger logger, String message)
CreatesExceptionHandler
that writes all exceptions to the specifiedlogger
with the specifiedmessage
.static ExceptionHandler
log(org.slf4j.Logger logger, Supplier<String> message)
CreatesExceptionHandler
that writes all exceptions to the specifiedlogger
with lazily evaluatedmessage
.
-
-
-
Method Detail
-
log
public static ExceptionHandler log()
ReturnsExceptionHandler
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. 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
Error
s. IfInterruptedException
is caught,Thread.interrupt()
is called.- Returns:
- logging exception handler
- See Also:
-
log(Logger)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger)
CreatesExceptionHandler
that 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
Error
s. IfInterruptedException
is caught,Thread.interrupt()
is called.- Parameters:
-
logger
- where all exceptions are logged - Returns:
- exception handler with custom logger
- Throws:
-
NullPointerException
- iflogger
isnull
- See Also:
-
log()
,log(Logger, String)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger, String message)
CreatesExceptionHandler
that writes all exceptions to the specifiedlogger
with 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
Error
s. IfInterruptedException
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
- iflogger
ormessage
isnull
- See Also:
-
log(Logger)
,log(Logger, Supplier)
-
log
public static ExceptionHandler log(org.slf4j.Logger logger, Supplier<String> message)
CreatesExceptionHandler
that writes all exceptions to the specifiedlogger
with 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. Ifmessage
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
Error
s. IfInterruptedException
is caught,Thread.interrupt()
is called.- Parameters:
-
logger
- where all exceptions are logged -
message
- aSupplier
that is lazily evaluated to generate log message - Returns:
- exception handler with custom logger and lazily evaluated message
- Throws:
-
NullPointerException
- iflogger
ormessage
isnull
- See Also:
-
log(Logger, String)
-
-