Class FlightRecorder


  • public class FlightRecorder
    extends java.lang.Object
    Fast in-memory log utilizing a cyclic buffer of a limited size. Intended to be used when alternative logging methods are too slow, for example, in the following scenario:
    • There is an expected behavior in the code, possibly caused by a race condition.
    • You add logging to the code to investigate the problem.
    • The problem is no longer reproducible with logging enabled.

    You may try to replace the regular logging with:

         FlightRecorder.initialize(100);
         ...
         FlightRecorder.log(something);
         ...
         FlightRecorder.log(somethingElse);
         ...
         if (unexpectedBehaviorDetected) {
             FlightRecorder.print();
         }
     
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.lang.Object> getAndClear()
      Returns the contents of the log and clears the log.
      static void initialize​(int sizeLimit)
      Initializes the log.
      static void log​(java.lang.Object record)
      Writes a record to the log if it was initialized.
      static void log​(java.util.function.Supplier<?> lazyRecord)
      Writes a record to the log if it was initialized.
      static void print()
      Prints the contents of the log to stdout and clears the log.
      • Methods inherited from class java.lang.Object

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

      • initialize

        public static void initialize​(int sizeLimit)
        Initializes the log. At most last sizeLimit records will be preserved in the log. The prior contents of the log up to sizeLimit records are preserved.
        Parameters:
        sizeLimit - the maximum number of records to store
      • log

        public static void log​(@NonNull
                               java.lang.Object record)
        Writes a record to the log if it was initialized.
        Parameters:
        record - an arbitrary object to log
      • log

        public static void log​(@NonNull
                               java.util.function.Supplier<?> lazyRecord)
        Writes a record to the log if it was initialized. The object being logged is evaluated only if the log was initialized with a non-zero size.
        Parameters:
        lazyRecord - the supplier of the object to log
      • getAndClear

        @NonNull
        public static java.util.List<java.lang.Object> getAndClear()
        Returns the contents of the log and clears the log.
      • print

        public static void print()
        Prints the contents of the log to stdout and clears the log.