Package uk.ac.ebi.utils.exceptions
Class ExceptionLogger
java.lang.Object
uk.ac.ebi.utils.exceptions.ExceptionLogger
An helper to log exceptions with double level log events, error and debug.
This is a simple utility to log an exception by producing one log event at 'error' level that only
shows a user-provided message and reports the exception's message
, followed
by a debug event that shows the same user message plus the exception's
stack trace
. As you can imagine this is useful when you have log targets
with different granularity reporting, eg, a console log at 'warning' level, plus a 'detailed.log' file at
debug level.
getLogger()
methods are provided, so that you can use this class the same way common logging
libraries are used, eg, define a field like exlog = ExceptionLogger.getLogger(...)
and then use
it in the class code.
- Author:
- brandizi
- Date:
- 22 Aug 2022
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExceptionLogger
The default logger, seegetLogger()
.static ExceptionLogger
static ExceptionLogger
static ExceptionLogger
getLogger
(org.slf4j.Logger logger) If null, uses a default that is based onObject.getClass()
.void
logEx
(String baseMessage, String errorMsgTail, String debugMsgTail, Throwable ex, Object... msgParams) Does the exception logging, as explained above.void
Defaults to reporting your message (instantiated with your params), plus sensible tails like ".
-
Method Details
-
getLogger
If null, uses a default that is based onObject.getClass()
.- See Also:
-
LoggerFactory
-
getLogger
-
getLogger
-
getLogger
The default logger, seegetLogger()
. -
logEx
public void logEx(String baseMessage, String errorMsgTail, String debugMsgTail, Throwable ex, Object... msgParams) Does the exception logging, as explained above.- Parameters:
baseMessage
- A base message. This can contain '{}' placeholders as usually. If it does, they must match msgParams.errorMsgTail
- a tail that is used for the error level log message. This usually contains a last '{}' placeholder, which is filled with theexception message
.debugMsgTail
- a tail that is used for the debug level log. This usually hasn't any placeholder, since we automatically add the exception ex to theLogger.debug(String, Object...)
and this reports the stacktrace, no matter if you have used a placeholder in the message or not.ex
- the exception that you want to logmsgParams
- the parameters to be used in the message placeholders. WARNING: as explained above, the final message for the logger is baseMessage plus errorMsgTail or debugMsgTail, and the logger is invoked with msgParams extended with either the exception message or the exception object. So, you've to consider that the last '{}' in the message (usually set in the tails) is for the exception message, NOT for your parameters. Moreover, the debug message reports the exception details anyway, as mentioned above.
-
logEx
Defaults to reporting your message (instantiated with your params), plus sensible tails like ". Error: {}" for the error level entry and ". Details:" for the debug entry. As explained, we don't need a placeholder for the exception stacktrace. since SLF4J reports it anyway.
-