Package com.bw.jtools

Class Log


  • public final class Log
    extends java.lang.Object
    Simple log handling.
    In most projects at some early point you have to decide which logging framework should be used.
    Often this decision is forced because of libraries you need and that already use some logging framework.
    I personally don't like to bind my source sp deeply to such stuff.
    The used logging framework is mostly not determined by functional requirements, so why bind a huge part of the sources to it?

    This class is a lightweight layer to keep the impact low.
    All methods are static!
    Pro: There is no need to store a "logger"-Instance in your class or to call singleton-getter each time.
    Contra: No way to specify different settings for different classes by configuration.
    I personally never needed to configure the logger differently for different parts of my software, so I can live with this restriction. And noise from other libraries can turned off by the used framework.
    If you use the other parts of this library that use this log, you can simply delegate the output to what-ever you like or turn it off.

    Current implementation tries to initialize Log4J2 as back-end. If Log4J2 is not found in class-path, a simple console-logger is used.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Log.LoggerFacade
      Adapter for logging back-ends.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static CollectorLogger collectorLog  
      static int DEBUG
      This logging level enables all output.
      static int ERROR
      This logging level enables only errors.
      static int INFO
      This logging level enables errors,warnings and information.
      static Log.LoggerFacade log  
      static int NONE
      This logging level turns all output off.
      static int WARN
      This logging level enables errors and warnings.
    • Constructor Summary

      Constructors 
      Constructor Description
      Log()  
    • Method Summary

      Modifier and Type Method Description
      static void addLogger​(Log.LoggerFacade logger)
      Adds a application specific logger.
      static void debug​(java.lang.CharSequence msg)
      Logs a debug message.
      static void debug​(java.lang.CharSequence msg, java.lang.Throwable t)
      Logs a debug message plus stack-trace.
      static void error​(java.lang.CharSequence msg)
      Logs an error.
      static void error​(java.lang.CharSequence msg, java.lang.Throwable t)
      Logs an error plus stack-trace.
      static java.lang.String getRestrictedStackTrace​(java.lang.Throwable t, java.lang.String prefix, int max_lines)
      Get a stack-trace with restricted length.
      static void info​(java.lang.CharSequence msg)
      Logs an informational message.
      static boolean isDebugEnabled()
      Checks if debug log-output is enabled.
      static boolean isErrorEnabled()
      Checks if error log-output is enabled.
      static boolean isInfoEnabled()
      Checks if informational log-output is enabled.
      static boolean isWarnEnabled()
      Checks if warning log-output is enabled.
      static void removeLogger​(Log.LoggerFacade logger)
      Removes a application specific logger.
      static void setLevel​(int level)
      Sets system log level.
      The value can be one of the constants NONE ERROR WARN INFO DEBUG
      static void startCollectMessages​(int level, boolean includeStackTraces)
      Starts collecting messages for the current thread.
      From this point until stopCollectMessages is called, all output in this thread that match the log.level is collected.
      This function was designed to provide "internal detail" data for some short-term operation.
      static void stopCollectMessages​(java.util.List<java.lang.String> messages)
      Stops collecting messages.
      All messages that were collected from the last call to startCollectMessages are returned.
      static void warn​(java.lang.CharSequence msg)
      Logs a warning.
      static void warn​(java.lang.CharSequence msg, java.lang.Throwable t)
      Logs an warning plus stack-trace.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Log

        public Log()
    • Method Detail

      • getRestrictedStackTrace

        public static java.lang.String getRestrictedStackTrace​(java.lang.Throwable t,
                                                               java.lang.String prefix,
                                                               int max_lines)
        Get a stack-trace with restricted length.
        Parameters:
        t - The Throwable for the stack-trace.
        prefix - Prefix for each lines, e.g. to add some indentation. Can be null.
        max_lines - Maximum number of lines to print.
        Returns:
        The stack-trace.
      • startCollectMessages

        public static void startCollectMessages​(int level,
                                                boolean includeStackTraces)
        Starts collecting messages for the current thread.
        From this point until stopCollectMessages is called, all output in this thread that match the log.level is collected.
        This function was designed to provide "internal detail" data for some short-term operation.
        Parameters:
        level - The level to collect for.
        includeStackTraces - If Stack-Traces should be included.
      • stopCollectMessages

        public static void stopCollectMessages​(java.util.List<java.lang.String> messages)
        Stops collecting messages.
        All messages that were collected from the last call to startCollectMessages are returned.
        Parameters:
        messages - List to add the messages to.
      • addLogger

        public static void addLogger​(Log.LoggerFacade logger)
        Adds a application specific logger.
        Parameters:
        logger - The logger to add.
      • removeLogger

        public static void removeLogger​(Log.LoggerFacade logger)
        Removes a application specific logger.
        Parameters:
        logger - The logger to remove.
      • setLevel

        public static void setLevel​(int level)
        Sets system log level.
        The value can be one of the constants
        • NONE
        • ERROR
        • WARN
        • INFO
        • DEBUG
        Parameters:
        level - The new system log level.
      • error

        public static void error​(java.lang.CharSequence msg)
        Logs an error.
        Parameters:
        msg - The message to log.
      • error

        public static void error​(java.lang.CharSequence msg,
                                 java.lang.Throwable t)
        Logs an error plus stack-trace.
        Parameters:
        msg - The message to log.
        t - The Throwable to trace.
      • warn

        public static void warn​(java.lang.CharSequence msg)
        Logs a warning.
        Parameters:
        msg - The message to log.
      • warn

        public static void warn​(java.lang.CharSequence msg,
                                java.lang.Throwable t)
        Logs an warning plus stack-trace.
        Parameters:
        msg - The message to log.
        t - The Throwable to trace.
      • info

        public static void info​(java.lang.CharSequence msg)
        Logs an informational message.
        Parameters:
        msg - The message to log.
      • debug

        public static void debug​(java.lang.CharSequence msg)
        Logs a debug message.
        Parameters:
        msg - The message to log.
      • debug

        public static void debug​(java.lang.CharSequence msg,
                                 java.lang.Throwable t)
        Logs a debug message plus stack-trace.
        Parameters:
        msg - The message to log.
        t - The Throwable to trace.
      • isDebugEnabled

        public static boolean isDebugEnabled()
        Checks if debug log-output is enabled.
        Returns:
        true if current log-level is equal to or higher than DEBUG.
      • isInfoEnabled

        public static boolean isInfoEnabled()
        Checks if informational log-output is enabled.
        Returns:
        true if current log-level is equal to or higher than INFO.
      • isWarnEnabled

        public static boolean isWarnEnabled()
        Checks if warning log-output is enabled.
        Returns:
        true if current log-level is equal to or higher than WARN.
      • isErrorEnabled

        public static boolean isErrorEnabled()
        Checks if error log-output is enabled.
        Returns:
        true if current log-level is equal to or higher than WARN.