Class LogNode

java.lang.Object
nonapi.io.github.classgraph.utils.LogNode

public final class LogNode extends 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 Details

    • log

      private static final 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 String msg
      The log message.
    • stackTrace

      private 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 Map<String,LogNode> children
      The child nodes of this log node.
    • sortKeyPrefix

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

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

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

      private static final 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 Details

    • LogNode

      private LogNode(String sortKey, String msg, long elapsedTimeNanos, 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 Details

    • 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(String timeStampStr, int indentLevel, String line, 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, StringBuilder buf)
      Recursively build the log output.
      Parameters:
      indentLevel - the indent level
      buf - the buf
    • toString

      public String toString()
      Build the log output. Call this on the toplevel log node.
      Overrides:
      toString in class 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(String sortKey, String msg, long elapsedTimeNanos, 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(String sortKey, 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(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(String sortKey, String msg, long elapsedTimeNanos, 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(String sortKey, 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(String sortKey, String msg, 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(String sortKey, 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(String msg, long elapsedTimeNanos, 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(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(String msg, 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(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(Collection<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(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.