Class LogNode


  • public final class LogNode
    extends java.lang.Object
    A tree-structured threadsafe log that allows you to add log entries in arbitrary order, and have the output retain a sane order. The order may also be made deterministic by specifying a sort key for log entries.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​LogNode> children
      The child nodes of this log node.
      private static java.text.SimpleDateFormat dateTimeFormatter
      The date/time formatter (not threadsafe).
      private long elapsedTimeNanos
      The time between when this log entry was created and addElapsedTime() was called.
      private static java.util.logging.Logger log
      The logger.
      private static boolean logInRealtime
      If true, log entries are output in realtime, as well as added to the LogNode tree.
      private java.lang.String msg
      The log message.
      private static java.text.DecimalFormat nanoFormatter
      The elapsed time formatter.
      private LogNode parent
      The parent LogNode.
      private java.lang.String sortKeyPrefix
      The sort key prefix for deterministic ordering of log entries.
      private static java.util.concurrent.atomic.AtomicInteger sortKeyUniqueSuffix
      The sort key suffix for this log entry, used to make sort keys unique.
      private java.lang.String stackTrace
      The stacktrace, if this log entry was due to an exception.
      private long timeStampMillis
      The timestamp at which the log node was created, in epoch millis.
      private long timeStampNano
      The timestamp at which the log node was created (relative to some arbitrary system timepoint).
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        LogNode()
      Create a toplevel log node.
      private LogNode​(java.lang.String sortKey, java.lang.String msg, long elapsedTimeNanos, java.lang.Throwable exception)
      Create a non-toplevel log node.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private LogNode addChild​(java.lang.String sortKey, java.lang.String msg, long elapsedTimeNanos)
      Add a child log node for a message.
      private LogNode addChild​(java.lang.String sortKey, java.lang.String msg, long elapsedTimeNanos, java.lang.Throwable exception)
      Add a child log node.
      private LogNode addChild​(java.lang.Throwable exception)
      Add a child log node for an exception.
      void addElapsedTime()
      Call this once the work corresponding with a given log entry has completed if you want to show the time taken after the log entry.
      private void appendLine​(java.lang.String timeStampStr, int indentLevel, java.lang.String line, java.lang.StringBuilder buf)
      Append a line to the log output, indenting this log entry according to tree structure.
      void flush()
      Flush out the log to stderr, and clear the log contents.
      LogNode log​(java.lang.String msg)
      Add a log entry.
      LogNode log​(java.lang.String msg, long elapsedTimeNanos)
      Add a log entry.
      LogNode log​(java.lang.String msg, long elapsedTimeNanos, java.lang.Throwable e)
      Add a log entry.
      LogNode log​(java.lang.String sortKey, java.lang.String msg)
      Add a log entry with sort key for deterministic ordering.
      LogNode log​(java.lang.String sortKey, java.lang.String msg, long elapsedTimeNanos)
      Add a log entry with sort key for deterministic ordering.
      LogNode log​(java.lang.String sortKey, java.lang.String msg, long elapsedTimeNanos, java.lang.Throwable e)
      Add a log entry with sort key for deterministic ordering.
      LogNode log​(java.lang.String sortKey, java.lang.String msg, java.lang.Throwable e)
      Add a log entry with sort key for deterministic ordering.
      LogNode log​(java.lang.String msg, java.lang.Throwable e)
      Add a log entry.
      LogNode log​(java.lang.Throwable e)
      Add a log entry.
      LogNode log​(java.util.Collection<java.lang.String> msgs)
      Add a series of log entries.
      static void logInRealtime​(boolean logInRealtime)
      If logInRealtime is true, log entries are output in realtime, as well as added to the LogNode tree.
      private void logJavaInfo()
      Log the Java version and the JRE paths that were found.
      java.lang.String toString()
      Build the log output.
      private void toString​(int indentLevel, java.lang.StringBuilder buf)
      Recursively build the log output.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • log

        private static final java.util.logging.Logger log
        The logger.
      • timeStampNano

        private final long timeStampNano
        The timestamp at which the log node was created (relative to some arbitrary system timepoint).
      • timeStampMillis

        private final long timeStampMillis
        The timestamp at which the log node was created, in epoch millis.
      • msg

        private final java.lang.String msg
        The log message.
      • stackTrace

        private java.lang.String stackTrace
        The stacktrace, if this log entry was due to an exception.
      • elapsedTimeNanos

        private long elapsedTimeNanos
        The time between when this log entry was created and addElapsedTime() was called.
      • parent

        private LogNode parent
        The parent LogNode.
      • children

        private final java.util.Map<java.lang.String,​LogNode> children
        The child nodes of this log node.
      • sortKeyPrefix

        private final java.lang.String sortKeyPrefix
        The sort key prefix for deterministic ordering of log entries.
      • sortKeyUniqueSuffix

        private static java.util.concurrent.atomic.AtomicInteger sortKeyUniqueSuffix
        The sort key suffix for this log entry, used to make sort keys unique.
      • dateTimeFormatter

        private static final java.text.SimpleDateFormat dateTimeFormatter
        The date/time formatter (not threadsafe).
      • nanoFormatter

        private static final java.text.DecimalFormat nanoFormatter
        The elapsed time formatter.
      • logInRealtime

        private static boolean logInRealtime
        If true, log entries are output in realtime, as well as added to the LogNode tree.
    • Constructor Detail

      • LogNode

        private LogNode​(java.lang.String sortKey,
                        java.lang.String msg,
                        long elapsedTimeNanos,
                        java.lang.Throwable exception)
        Create a non-toplevel log node. The order may also be made deterministic by specifying a sort key for log entries.
        Parameters:
        sortKey - the sort key
        msg - the log message
        elapsedTimeNanos - the elapsed time in nanos
        exception - the exception that was thrown
      • LogNode

        public LogNode()
        Create a toplevel log node.
    • Method Detail

      • logInRealtime

        public static void logInRealtime​(boolean logInRealtime)
        If logInRealtime is true, log entries are output in realtime, as well as added to the LogNode tree. This can help debug situations where log info is never shown, e.g. deadlocks, or where you need to show the log info right up to the point where you hit a breakpoint.
        Parameters:
        logInRealtime - whether to log in realtime
      • logJavaInfo

        private void logJavaInfo()
        Log the Java version and the JRE paths that were found.
      • appendLine

        private void appendLine​(java.lang.String timeStampStr,
                                int indentLevel,
                                java.lang.String line,
                                java.lang.StringBuilder buf)
        Append a line to the log output, indenting this log entry according to tree structure.
        Parameters:
        timeStampStr - the timestamp string
        indentLevel - the indent level
        line - the line to log
        buf - the buf
      • toString

        private void toString​(int indentLevel,
                              java.lang.StringBuilder buf)
        Recursively build the log output.
        Parameters:
        indentLevel - the indent level
        buf - the buf
      • toString

        public java.lang.String toString()
        Build the log output. Call this on the toplevel log node.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string
      • addElapsedTime

        public void addElapsedTime()
        Call this once the work corresponding with a given log entry has completed if you want to show the time taken after the log entry.
      • addChild

        private LogNode addChild​(java.lang.String sortKey,
                                 java.lang.String msg,
                                 long elapsedTimeNanos,
                                 java.lang.Throwable exception)
        Add a child log node.
        Parameters:
        sortKey - the sort key
        msg - the log message
        elapsedTimeNanos - the elapsed time in nanos
        exception - the exception that was thrown
        Returns:
        the log node
      • addChild

        private LogNode addChild​(java.lang.String sortKey,
                                 java.lang.String msg,
                                 long elapsedTimeNanos)
        Add a child log node for a message.
        Parameters:
        sortKey - the sort key
        msg - the log message
        elapsedTimeNanos - the elapsed time in nanos
        Returns:
        the log node
      • addChild

        private LogNode addChild​(java.lang.Throwable exception)
        Add a child log node for an exception.
        Parameters:
        exception - the exception that was thrown
        Returns:
        the log node
      • log

        public LogNode log​(java.lang.String sortKey,
                           java.lang.String msg,
                           long elapsedTimeNanos,
                           java.lang.Throwable e)
        Add a log entry with sort key for deterministic ordering.
        Parameters:
        sortKey - The sort key for the log entry.
        msg - The message.
        elapsedTimeNanos - The elapsed time.
        e - The Throwable that was thrown.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String sortKey,
                           java.lang.String msg,
                           long elapsedTimeNanos)
        Add a log entry with sort key for deterministic ordering.
        Parameters:
        sortKey - The sort key for the log entry.
        msg - The message.
        elapsedTimeNanos - The elapsed time.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String sortKey,
                           java.lang.String msg,
                           java.lang.Throwable e)
        Add a log entry with sort key for deterministic ordering.
        Parameters:
        sortKey - The sort key for the log entry.
        msg - The message.
        e - The Throwable that was thrown.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String sortKey,
                           java.lang.String msg)
        Add a log entry with sort key for deterministic ordering.
        Parameters:
        sortKey - The sort key for the log entry.
        msg - The message.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String msg,
                           long elapsedTimeNanos,
                           java.lang.Throwable e)
        Add a log entry.
        Parameters:
        msg - The message.
        elapsedTimeNanos - The elapsed time.
        e - The Throwable that was thrown.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String msg,
                           long elapsedTimeNanos)
        Add a log entry.
        Parameters:
        msg - The message.
        elapsedTimeNanos - The elapsed time.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String msg,
                           java.lang.Throwable e)
        Add a log entry.
        Parameters:
        msg - The message.
        e - The Throwable that was thrown.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.String msg)
        Add a log entry.
        Parameters:
        msg - The message.
        Returns:
        a child log node, which can be used to add sub-entries.
      • log

        public LogNode log​(java.util.Collection<java.lang.String> msgs)
        Add a series of log entries. Returns the last LogNode created.
        Parameters:
        msgs - The messages.
        Returns:
        the last log node created, which can be used to add sub-entries.
      • log

        public LogNode log​(java.lang.Throwable e)
        Add a log entry.
        Parameters:
        e - The Throwable that was thrown.
        Returns:
        a child log node, which can be used to add sub-entries.
      • flush

        public void flush()
        Flush out the log to stderr, and clear the log contents. Only call this on the toplevel log node, when threads do not have access to references of internal log nodes so that they cannot add more log entries inside the tree, otherwise log entries may be lost.