Class ExceptionLogger

java.lang.Object
uk.ac.ebi.utils.exceptions.ExceptionLogger

public class ExceptionLogger extends Object
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 Details

    • getLogger

      public static ExceptionLogger getLogger(org.slf4j.Logger logger)
      If null, uses a default that is based on Object.getClass().
      See Also:
      • LoggerFactory
    • getLogger

      public static ExceptionLogger getLogger(String name)
    • getLogger

      public static ExceptionLogger getLogger(Class<?> cls)
    • getLogger

      public static ExceptionLogger getLogger()
      The default logger, see getLogger().
    • 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 the exception 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 the Logger.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 log
      msgParams - 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

      public void logEx(String baseMessage, Throwable ex, Object... msgParams)
      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.
      See Also: