Package org.jvnet.hudson.test
Class LogRecorder
java.lang.Object
org.jvnet.hudson.test.LogRecorder
- All Implemented Interfaces:
AutoCloseable
A test utility which allows you to easily enable one or more loggers for the duration of a test.
Call
record(Class, Level)
or another overload for the recording to take effect.
By default, messages are merely printed to test output.
If you also want to examine them, call capture(int)
.
See the following example:
try (LogRecorder recorder = new LogRecorder().record("Foo", Level.INFO).capture(100)) {
LOGGER.log(Level.INFO, "Log Message");
assertThat(recorder, LogRecorder.recorded(equalTo("Log Message")));
}
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncapture
(int maximum) Initializes log record capture, in addition to merely printing it.void
close()
Returns a read-only view of current messages.Obtains all log records collected so far during this test case.quiet()
Don't emit logs to the console, only record.Same asrecord(String, Level)
but callsClass.getName()
for you first.Same asrecord(Logger, Level)
but callsLogger.getLogger(String)
for you first.Start listening to a logger.static org.hamcrest.Matcher<LogRecorder>
Creates aMatcher
that matches if theLogRecorder
has aLogRecord
at the specifiedLevel
and with a message matching the specified matcher.static org.hamcrest.Matcher<LogRecorder>
Creates aMatcher
that matches if theLogRecorder
has aLogRecord
at the specifiedLevel
, with a message matching the specified matcher, and with aThrowable
matching the specified matcher.static org.hamcrest.Matcher<LogRecorder>
Creates aMatcher
that matches if theLogRecorder
has aLogRecord
with a message matching the specified matcher.static org.hamcrest.Matcher<LogRecorder>
Creates aMatcher
that matches if theLogRecorder
has aLogRecord
with a message matching the specified matcher and with aThrowable
matching the specified matcher.recordPackage
(Class<?> clazz, Level level) Same asrecord(String, Level)
but callsClass.getPackage()
and getName() for you first.toString()
-
Constructor Details
-
LogRecorder
public LogRecorder()Initializes the recorder, by default not recording anything.
-
-
Method Details
-
quiet
Don't emit logs to the console, only record. -
toString
-
capture
Initializes log record capture, in addition to merely printing it. This allows you to callgetRecords()
and/orgetMessages()
later.- Parameters:
maximum
- the maximum number of records to keep (any further will be discarded)- Returns:
- this recorder, for convenience
-
record
Start listening to a logger.- Parameters:
logger
- some loggerlevel
- something betweenLevel.CONFIG
andLevel.ALL
; usingLevel.INFO
or above is typically senseless, since Java will by default log everything at such levels anyway; unless you wish to inspect visiblegetRecords()
, or wish to suppress console log output for some logger- Returns:
- this recorder, for convenience
-
record
Same asrecord(Logger, Level)
but callsLogger.getLogger(String)
for you first. -
record
Same asrecord(String, Level)
but callsClass.getName()
for you first. -
recordPackage
Same asrecord(String, Level)
but callsClass.getPackage()
and getName() for you first. -
getRecords
Obtains all log records collected so far during this test case. You must have first calledcapture(int)
. If more than the maximum number of records were captured, older ones will have been discarded. -
getMessages
Returns a read-only view of current messages.Formatter.formatMessage(java.util.logging.LogRecord)
applied togetRecords()
at the time of logging. However, if the message is null, but there is an exception,Throwable.toString()
will be used. Does not include logger names, stack traces, times, etc. (these will appear in the test console anyway). -
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
recorded
public static org.hamcrest.Matcher<LogRecorder> recorded(@CheckForNull Level level, @NonNull org.hamcrest.Matcher<String> message, @CheckForNull org.hamcrest.Matcher<Throwable> thrown) Creates aMatcher
that matches if theLogRecorder
has aLogRecord
at the specifiedLevel
, with a message matching the specified matcher, and with aThrowable
matching the specified matcher. You must have first calledcapture(int)
.- Parameters:
level
- TheLevel
of theLogRecorder
to match. Passnull
to match anyLevel
.message
- the matcher to match againstLogRecord.getMessage()
thrown
- the matcher to match againstLogRecord.getThrown()
. Passingnull
is equivalent to passingMatchers.anything()
-
recorded
public static org.hamcrest.Matcher<LogRecorder> recorded(@CheckForNull Level level, @NonNull org.hamcrest.Matcher<String> message) Creates aMatcher
that matches if theLogRecorder
has aLogRecord
at the specifiedLevel
and with a message matching the specified matcher. You must have first calledcapture(int)
.- Parameters:
level
- TheLevel
of theLogRecorder
to match. Passnull
to match anyLevel
.message
- The matcher to match againstLogRecord.getMessage()
.
-
recorded
public static org.hamcrest.Matcher<LogRecorder> recorded(@NonNull org.hamcrest.Matcher<String> message, @CheckForNull org.hamcrest.Matcher<Throwable> thrown) Creates aMatcher
that matches if theLogRecorder
has aLogRecord
with a message matching the specified matcher and with aThrowable
matching the specified matcher. You must have first calledcapture(int)
.- Parameters:
message
- the matcher to match againstLogRecord.getMessage()
thrown
- the matcher to match againstLogRecord.getThrown()
. Passingnull
is equivalent to passingMatchers.anything()
-
recorded
public static org.hamcrest.Matcher<LogRecorder> recorded(@NonNull org.hamcrest.Matcher<String> message) Creates aMatcher
that matches if theLogRecorder
has aLogRecord
with a message matching the specified matcher. You must have first calledcapture(int)
.- Parameters:
message
- the matcher to match againstLogRecord.getMessage()
-