Class LoggingHelper


  • public class LoggingHelper
    extends Object
    Helper class for logging related stuff.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • LOGLEVEL_SUFFIX

        public static final String LOGLEVEL_SUFFIX
        the environment variable suffix of the log level to look for.
        See Also:
        Constant Field Values
      • m_LogLevelCache

        protected static Map<Class,​Level> m_LogLevelCache
        the cache for the loglevels.
      • m_LevelComparator

        protected static final LevelComparator m_LevelComparator
        for comparing levels.
      • m_DefaultHandler

        protected static Handler m_DefaultHandler
        the global logging handler.
      • m_DateFormat

        protected static DateFormat m_DateFormat
        the formatter for the timestamp.
    • Constructor Detail

      • LoggingHelper

        public LoggingHelper()
    • Method Detail

      • getLevel

        protected static Level getLevel​(String env)
        Gets the level for the specified environment variable. Checks: - as is - dots -> underscores - uppercase - dots -> underscores + uppercase
        Parameters:
        env - the environment variable to get the level for, if possible
        Returns:
        the determined level or null if nothing found or parsable
      • getLevel

        public static Level getLevel​(Class cls)
        Returns the log level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned. Default is WARNING. Instead of dots, environment variables with underscores are inspected as well (and uppercase), i.e., "hello_world_App_LOGLEVEL", "App_LOGLEVEL", "HELLO_WORLD_APP_LOGLEVEL", "APP_LOGLEVEL".
        Parameters:
        cls - the class to return the debug level for
        Returns:
        the logging level
      • getLevel

        public static Level getLevel​(Class cls,
                                     Level defLevel)
        Returns the log level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned. Instead of dots, environment variables with underscores are inspected as well (and uppercase), i.e., "hello_world_App_LOGLEVEL", "App_LOGLEVEL", "HELLO_WORLD_APP_LOGLEVEL", "APP_LOGLEVEL".
        Parameters:
        cls - the class to return the debug level for
        defLevel - the default level to use
        Returns:
        the logging level
      • getLoggingLevel

        public static LoggingLevel getLoggingLevel​(Class cls)
        Returns the logging level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned. Default is WARNING.
        Parameters:
        cls - the class to return the debug level for
        Returns:
        the logging level
      • getLoggingLevel

        public static LoggingLevel getLoggingLevel​(Class cls,
                                                   LoggingLevel defLevel)
        Returns the logging level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned.
        Parameters:
        cls - the class to return the debug level for
        defLevel - the default level
        Returns:
        the logging level
      • getLogger

        public static Logger getLogger​(Class cls)
        Returns the a logger with the log level for the specified class. "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned. Default level is WARNING.
        Parameters:
        cls - the class to return the logger for
        Returns:
        the logger
      • getLogger

        public static Logger getLogger​(Class cls,
                                       Level defLevel)
        Returns the a logger with the log level for the specified class. "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned.
        Parameters:
        cls - the class to return the logger for
        defLevel - the default level
        Returns:
        the logger
      • getLogger

        public static Logger getLogger​(String name)
        Returns the a logger with the specified name. Default level is WARNING.
        Parameters:
        name - the name of the class to return the logger for
        Returns:
        the logger
      • getLogger

        public static Logger getLogger​(String name,
                                       Level defLevel)
        Returns the a logger with the specified name.
        Parameters:
        name - the name of the class to return the logger for
        defLevel - the default level
        Returns:
        the logger
      • getConsoleLogger

        public static Logger getConsoleLogger​(Class cls)
        Returns a console logger with the log level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned. Default level is WARNING.
        Parameters:
        cls - the class to return the logger for
        Returns:
        the logger
      • getConsoleLogger

        public static Logger getConsoleLogger​(Class cls,
                                              Level defLevel)
        Returns a console logger with the log level for the specified class. E.g., for the class "hello.world.App" the environment variables "hello.world.App.LOGLEVEL" and "App.LOGLEVEL" are inspected and "{OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST}" returned.
        Parameters:
        cls - the class to return the logger for
        defLevel - the default level
        Returns:
        the logger
      • getConsoleLogger

        public static Logger getConsoleLogger​(String name)
        Returns a console logger with the log level for the specified class. Default level is WARNING.
        Parameters:
        name - the name of the class to return the logger for
        Returns:
        the logger
      • getConsoleLogger

        public static Logger getConsoleLogger​(String name,
                                              Level defLevel)
        Returns a console logger with the log level for the specified class.
        Parameters:
        name - the name of the class to return the logger for
        defLevel - the default level
        Returns:
        the logger
      • isAtLeast

        public static boolean isAtLeast​(Logger logger,
                                        Level levelMin)
        Checks whether the logger has at least the specified level set.
        Parameters:
        logger - the logger to check
        levelMin - the minimum level to meet
        Returns:
        if minimum logging level met
      • isAtLeast

        public static boolean isAtLeast​(Level level,
                                        Level levelMin)
        Checks whether the level meets the minimum.
        Parameters:
        level - the level to check
        levelMin - the minimum level to meet
        Returns:
        if minimum logging level met
      • isAtMost

        public static boolean isAtMost​(Logger logger,
                                       Level levelMax)
        Checks whether the logger has at most the specified level set.
        Parameters:
        logger - the logger to check
        levelMax - the maximum level to meet
        Returns:
        if maximum logging level met
      • isAtMost

        public static boolean isAtMost​(Level level,
                                       Level levelMax)
        Checks whether the level is at most the specified maximum level.
        Parameters:
        level - the level to check
        levelMax - the maximum level to meet
        Returns:
        if maximum logging level met
      • setDefaultHandler

        public static void setDefaultHandler​(Handler value)
        Sets the handler to use for logging.
        Parameters:
        value - the handler
      • getDefaultHandler

        public static Handler getDefaultHandler()
        Returns the current log handler.
        Returns:
        the handler
      • indexOfDefaultHandler

        public static int indexOfDefaultHandler​(Handler handler)
        Determines the index of the handler in the default handler.
        Parameters:
        handler - the handler to look for
        Returns:
        the index, -1 if not found
        See Also:
        getDefaultHandler()
      • addToDefaultHandler

        public static String addToDefaultHandler​(Handler handler)
        Adds the handler to the default handler, but only if not already present.
        Returns:
        null if successful, otherwise error message
        See Also:
        getDefaultHandler()
      • removeFromDefaultHandler

        public static String removeFromDefaultHandler​(Handler handler)
        Removes the handler from the default handler. Calls the 'close()' method of the handler if found.
        Returns:
        null if successful, otherwise error message
        See Also:
        getDefaultHandler()
      • wrapDefaultHandler

        public static String wrapDefaultHandler​(EnhancingSingleHandler wrapper)
        Wraps the default handler in the provided handler, but only if not already wrapped.
        From:
         MultiHandler
         - handler1
         - handler2
         - ...
         - handlerN
         
        To:
         MultiHandler
         - wrapper
            - MultiHandlerInner
              - handler1
              - handler2
              - ...
              - handlerN
         
        Parameters:
        wrapper - the handler to use for wrapping
        Returns:
        null if successful, otherwise error message
        See Also:
        getDefaultHandler()
      • unwrapDefaultHandler

        public static String unwrapDefaultHandler​(EnhancingSingleHandler wrapper)
        Removes the layer introduced by the provided handler in the default handler, but only if wrapped.
        From:
         MultiHandler
         - wrapper
            - MultiHandlerInner
              - handler1
              - handler2
              - ...
              - handlerN
         
        To:
         MultiHandler
         - handler1
         - handler2
         - ...
         - handlerN
         
        Parameters:
        wrapper - the wrapper handler to remove
        Returns:
        null if successful, otherwise error message
        See Also:
        getDefaultHandler()
      • useHandlerFromOptions

        public static boolean useHandlerFromOptions​(String[] options)
        Interprets the "-logging-handler <classname>" option in the command-line options and sets the logging handler accordingly.
        Parameters:
        options - the command-line options
        Returns:
        true if handler updated
      • outputHandlerOption

        public static void outputHandlerOption()
        Outputs the handler option definition on System.out.
      • getDateFormat

        protected static DateFormat getDateFormat()
        Returns the formatter for the timestamps.
        Returns:
        the formatter
      • assembleMessage

        public static StringBuilder assembleMessage​(LogRecord record)
        Publish a LogRecord.

        The logging request was made initially to a Logger object, which initialized the LogRecord and forwarded it here.

        The Handler is responsible for formatting the message, when and if necessary. The formatting should include localization.

        "{}" placeholders in the message get replaced with the objects from LogRecord.getParameters().

        Parameters:
        record - description of the log event. A null record is silently ignored and is not published
      • getMethodName

        public static String getMethodName()
        Returns the name of the method calling this method.
        Returns:
        the generated string
      • getMethodName

        public static String getMethodName​(boolean cls)
        Returns the name of the method calling this method.
        Parameters:
        cls - whether to include the class name
        Returns:
        the generated string
      • getMethodName

        public static String getMethodName​(boolean cls,
                                           boolean line)
        Returns the name of the method calling this method.
        Parameters:
        cls - whether to include the class name
        line - whether to include the line number
        Returns:
        the generated string
      • getLineNumber

        public static int getLineNumber()
        Returns the line number this method was called in.
        Returns:
        the line number
      • throwableToString

        public static String throwableToString​(Throwable t)
        Returns the stacktrace of the throwable as string.
        Parameters:
        t - the throwable to get the stacktrace for
        Returns:
        the stacktrace
      • throwableToString

        public static String throwableToString​(Throwable t,
                                               int maxLines)
        Returns the stacktrace of the throwable as string.
        Parameters:
        t - the throwable to get the stacktrace for
        maxLines - the maximum number of lines to print, <= 0 for all
        Returns:
        the stacktrace
      • getStackTrace

        public static String getStackTrace​(int maxDepth)
        Returns the current stack trace.
        Parameters:
        maxDepth - the maximum depth of the stack trace, <= 0 for full trace
        Returns:
        the stack trace as string (multiple lines)
      • handleException

        public static String handleException​(LoggingSupporter source,
                                             String msg,
                                             Throwable t)
        Outputs the stacktrace along with the message on stderr and returns a combination of both of them as string.
        Parameters:
        source - the object that generated the exception, can be null
        msg - the message for the exception
        t - the exception
        Returns:
        the full error message (message + stacktrace)
      • handleException

        public static String handleException​(LoggingSupporter source,
                                             String msg,
                                             Throwable t,
                                             boolean silent)
        Generates a string from the stacktrace along with the message and returns that. Depending on the silent flag, this string is also forwarded to the source's logger.
        Parameters:
        source - the object that generated the exception, can be null
        msg - the message for the exception
        t - the exception
        silent - if true then the generated message is not forwarded to the source's logger
        Returns:
        the full error message (message + stacktrace)