Class LogCollector

java.lang.Object
org.jtrim2.logs.LogCollector
All Implemented Interfaces:
AutoCloseable

public final class LogCollector extends Object implements AutoCloseable
Defines a class which is able to collect logs written to a java.util.logging.Logger. This class is generally useful when testing if a particular code emitted log message or not.

To instantiate this class call the startCollecting(String) factory method.

Warning: Instances of this class need to be closed in order to unregister log handlers. Failing to call the close method will cause a memory leak.

Thread safety

Methods of this class are safe to be used by multiple threads concurrently.

Synchronization transparency

Methods of this class are synchronization transparent unless otherwise noted.
See Also:
  • Method Details

    • startCollecting

      public static LogCollector startCollecting(String loggerName)
      Creats a new LogCollector which immediately starts listening for logs passed to the specified logger.

      This method will add a log handler to the specified logger and to remove this handler you have to call the close method of the returned LogCollector.

      Parameters:
      loggerName - the name of the logger whose logs are to be collected. This argument cannot be null.
      Returns:
      the LogCollector which will collect logs passed to the given logger. This method never returns null.
    • extractThrowables

      public static Throwable[] extractThrowables(Class<? extends Throwable> cl, Throwable... exceptions)
      Collects all the throwable extending the given class from the given Throwable instances. This method will also find exceptions within the causes and the suppressed exceptions of the passed exceptions (recursively).
      Parameters:
      cl - the class of the exceptions to be returned. This method will also return exceptions with exactly the same type. That is, calling this method with Throwable.class will return every possible exception. This argument cannot be null.
      exceptions - the exceptions to be searched for the specified type of exceptions. This array cannot be null but its elements can be null, null elements will be ignored.
      Returns:
      the exceptions implementing the given type. The returned array will not contain the same (reference comparison) exception multiple times. This method never returns null.
    • getNumberOfLogs

      public int getNumberOfLogs()
      Returns the number of log records collected. This is effectively equivalent to getLogs().length but is more efficient.
      Returns:
      the number of log records collected. This method always returns a value greater than or equal to zero.
    • getNumberOfLogs

      public int getNumberOfLogs(Level level)
      Returns the number of log records collected having the specified log level. The level must have an exact match, so passing Level.SEVERE will not count Level.WARNING logs.
      Parameters:
      level - the level of the logs to be counted. This argument cannot be null.
      Returns:
      the number of log records collected having the specified log level. This method always returns a value greater than or equal to zero.
    • getLogs

      public LogRecord[] getLogs()
      Returns all the collected logs. This method returns the log records in the order they were collected.
      Returns:
      all the collected logs. This method never returns null.
    • getExceptions

      public Throwable[] getExceptions(Level level)
      Returns all the exceptions attached to the collected log records having the specified log level. The exceptions are extracted from the thrown property of the log records. The level must have an exact match, so passing Level.SEVERE will not consider Level.WARNING logs.
      Parameters:
      level - the level of the logs to be checked. This argument cannot be null.
      Returns:
      all the exceptions attached to the collected log records having the specified log level. This method never returns null.
    • close

      public void close()
      Removes the log handler the LogCollector uses and stops collecting logs. Logging after this method call will have no effect on this LogCollector.

      This method is idempotent, so calling it multiple times has no further effect.

      Specified by:
      close in interface AutoCloseable