Class DebugLogger


  • public class DebugLogger
    extends java.lang.Object
    Often times when trying to understand a concurrency issue, adding logging may solve that problem. This class is designed to help work around that problem in some situations. It works by not actually outputting the logs collected, but storing them in a concurrent structure.

    t will ensure that when your ready to dump all the logs, they will be returned in the order they were provided. Since these are not outputted to the actual log stream, make sure any logging relevant to the issue is captured by this utility.

    This utility has several deficiencies, the largest of which is using System.nanoTime() for log ordering. Since nanosecond time can roll over from positive to negative, in those rare situations log ordering may be incorrect. It is design only as a debugging aid and should NEVER be included after debugging is completed.

    Since:
    1.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      DebugLogger()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getAllStoredMessages()
      Request to get and clear all currently stored log messages.
      static java.lang.String getAllStoredMessages​(boolean includeLogTimes)
      Request to get and clear all currently stored log messages.
      static int getCurrentMessageQty()
      This call checks how many messages are waiting in the stored map.
      static java.lang.String getOldestLogMessages​(int qty)
      This call retrieves and removes the oldest stored log messages.
      static java.lang.String getOldestLogMessages​(int qty, boolean includeLogTimes)
      This call retrieves and removes the oldest stored log messages.
      static void log​(java.lang.String msg)
      This adds a log message to the stored log.
      • Methods inherited from class java.lang.Object

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

      • DebugLogger

        public DebugLogger()
    • Method Detail

      • log

        public static void log​(java.lang.String msg)
        This adds a log message to the stored log. Keep in mind this will continue to consume more and more memory until getAllStoredMessages() is called.
        Parameters:
        msg - message to be stored into log map
      • getCurrentMessageQty

        public static int getCurrentMessageQty()
        This call checks how many messages are waiting in the stored map. This can be useful if you are possibly storing lots of messages and may need to know when to regularly drain the map.
        Returns:
        current number of stored log messages
      • getAllStoredMessages

        public static java.lang.String getAllStoredMessages()
        Request to get and clear all currently stored log messages. This will return all the log messages formatted into a single string, separated by new line characters.

        This calls getAllStoredMessages(boolean) with a default of NOT including the time in nanoseconds.

        Returns:
        string with all log messages, separated by a new line
      • getAllStoredMessages

        public static java.lang.String getAllStoredMessages​(boolean includeLogTimes)
        Request to get and clear all currently stored log messages. This will return all the log messages formatted into a single string, separated by new line characters.
        Parameters:
        includeLogTimes - boolean to include time in nanoseconds that log message was recorded
        Returns:
        string with all log messages, separated by a new line
      • getOldestLogMessages

        public static java.lang.String getOldestLogMessages​(int qty)
        This call retrieves and removes the oldest stored log messages. It will only return at most the maximum quantity provided, but may return less if not that many messages are currently available. This call is slightly less efficient than getAllStoredMessages().

        This calls getOldestLogMessages(int, boolean) with a default of NOT including the time in nanoseconds.

        Parameters:
        qty - maximum quantity of messages to retrieve
        Returns:
        string with requested log messages, separated by a new line
      • getOldestLogMessages

        public static java.lang.String getOldestLogMessages​(int qty,
                                                            boolean includeLogTimes)
        This call retrieves and removes the oldest stored log messages. It will only return at most the maximum quantity provided, but may return less if not that many messages are currently available. This call is slightly less efficient than getAllStoredMessages().
        Parameters:
        qty - maximum quantity of messages to retrieve
        includeLogTimes - boolean to include time in nanoseconds that log message was recorded
        Returns:
        string with requested log messages, separated by a new line