Package org.jtrim2.logs
Class LogCollector
java.lang.Object
org.jtrim2.logs.LogCollector
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescriptionvoid
close()
Removes the log handler theLogCollector
uses and stops collecting logs.static Throwable[]
extractThrowables
(Class<? extends Throwable> cl, Throwable... exceptions) Collects all the throwable extending the given class from the givenThrowable
instances.getExceptions
(Level level) Returns all the exceptions attached to the collected log records having the specified log level.getLogs()
Returns all the collected logs.int
Returns the number of log records collected.int
getNumberOfLogs
(Level level) Returns the number of log records collected having the specified log level.static LogCollector
startCollecting
(String loggerName) Creats a newLogCollector
which immediately starts listening for logs passed to the specified logger.
-
Method Details
-
startCollecting
Creats a newLogCollector
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 returnedLogCollector
.- Parameters:
loggerName
- the name of the logger whose logs are to be collected. This argument cannot benull
.- Returns:
- the
LogCollector
which will collect logs passed to the given logger. This method never returnsnull
.
-
extractThrowables
Collects all the throwable extending the given class from the givenThrowable
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 withThrowable.class
will return every possible exception. This argument cannot benull
.exceptions
- the exceptions to be searched for the specified type of exceptions. This array cannot benull
but its elements can benull
,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 togetLogs().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
Returns the number of log records collected having the specified log level. The level must have an exact match, so passingLevel.SEVERE
will not countLevel.WARNING
logs.- Parameters:
level
- the level of the logs to be counted. This argument cannot benull
.- 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
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
Returns all the exceptions attached to the collected log records having the specified log level. The exceptions are extracted from thethrown
property of the log records. The level must have an exact match, so passingLevel.SEVERE
will not considerLevel.WARNING
logs.- Parameters:
level
- the level of the logs to be checked. This argument cannot benull
.- 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 theLogCollector
uses and stops collecting logs. Logging after this method call will have no effect on thisLogCollector
.This method is idempotent, so calling it multiple times has no further effect.
- Specified by:
close
in interfaceAutoCloseable
-