public class LoggerRule
extends org.junit.rules.ExternalResource
record(Class, Level) or another overload for the rule to take effect.
By default messages are merely printed to test output.
If you also want to examine them, call capture(int).
To print and/or capture messages during Jenkins startup,
you may compose this with a JenkinsRule using a RuleChain;
or use as a ClassRule.
| Constructor and Description |
|---|
LoggerRule()
Initializes the rule, by default not recording anything.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
after() |
LoggerRule |
capture(int maximum)
Initializes log record capture, in addition to merely printing it.
|
List<String> |
getMessages()
Returns a read-only view of current messages.
|
List<LogRecord> |
getRecords()
Obtains all log records collected so far during this test case.
|
LoggerRule |
record(Class<?> clazz,
Level level)
Same as
record(String, Level) but calls Class.getName() for you first. |
LoggerRule |
record(Logger logger,
Level level)
Start listening to a logger.
|
LoggerRule |
record(String name,
Level level)
Same as
record(Logger, Level) but calls Logger.getLogger(String) for you first. |
static org.hamcrest.Matcher<LoggerRule> |
recorded(Level level,
org.hamcrest.Matcher<String> message)
Creates a
Matcher that matches if the LoggerRule has a LogRecord at
the specified Level and with a message matching the specified matcher. |
static org.hamcrest.Matcher<LoggerRule> |
recorded(Level level,
org.hamcrest.Matcher<String> message,
org.hamcrest.Matcher<Throwable> thrown)
Creates a
Matcher that matches if the LoggerRule has a LogRecord at
the specified Level, with a message matching the specified matcher, and with a
Throwable matching the specified matcher. |
static org.hamcrest.Matcher<LoggerRule> |
recorded(org.hamcrest.Matcher<String> message)
Creates a
Matcher that matches if the LoggerRule has a LogRecord
with a message matching the specified matcher. |
static org.hamcrest.Matcher<LoggerRule> |
recorded(org.hamcrest.Matcher<String> message,
org.hamcrest.Matcher<Throwable> thrown)
Creates a
Matcher that matches if the LoggerRule has a LogRecord
with a message matching the specified matcher and with a Throwable matching the specified
matcher. |
LoggerRule |
recordPackage(Class<?> clazz,
Level level)
Same as
record(String, Level) but calls Class.getPackage() and getName() for you first. |
public LoggerRule()
public LoggerRule capture(int maximum)
getRecords() and/or getMessages() later.maximum - the maximum number of records to keep (any further will be discarded)public LoggerRule record(Logger logger, Level level)
Rule initializer, to apply to all test cases in a suite;
or only at the start of selected test cases.logger - some loggerlevel - something between Level.CONFIG and Level.ALL;
using Level.INFO or above is typically senseless,
since Java will by default log everything at such levels anyway;
unless you wish to inspect visible getRecords(),
or wish to suppress console log output for some loggerpublic LoggerRule record(String name, Level level)
record(Logger, Level) but calls Logger.getLogger(String) for you first.public LoggerRule record(Class<?> clazz, Level level)
record(String, Level) but calls Class.getName() for you first.public LoggerRule recordPackage(Class<?> clazz, Level level)
record(String, Level) but calls Class.getPackage() and getName() for you first.public List<LogRecord> getRecords()
capture(int).
If more than the maximum number of records were captured, older ones will have been discarded.public List<String> getMessages()
Formatter.formatMessage(java.util.logging.LogRecord) applied to getRecords() 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).protected void after()
after in class org.junit.rules.ExternalResourcepublic static org.hamcrest.Matcher<LoggerRule> recorded(@CheckForNull Level level, @Nonnull org.hamcrest.Matcher<String> message, @CheckForNull org.hamcrest.Matcher<Throwable> thrown)
Matcher that matches if the LoggerRule has a LogRecord at
the specified Level, with a message matching the specified matcher, and with a
Throwable matching the specified matcher.
You must have first called capture(int).level - The Level of the LoggerRule to match. Pass null to match any Level.message - the matcher to match against LogRecord.getMessage()thrown - the matcher to match against LogRecord.getThrown(). Passing null is equivalent to
passing CoreMatchers.anything()public static org.hamcrest.Matcher<LoggerRule> recorded(@CheckForNull Level level, @Nonnull org.hamcrest.Matcher<String> message)
Matcher that matches if the LoggerRule has a LogRecord at
the specified Level and with a message matching the specified matcher.
You must have first called capture(int).level - The Level of the LoggerRule to match. Pass null to match any Level.message - The matcher to match against LogRecord.getMessage().public static org.hamcrest.Matcher<LoggerRule> recorded(@Nonnull org.hamcrest.Matcher<String> message, @CheckForNull org.hamcrest.Matcher<Throwable> thrown)
Matcher that matches if the LoggerRule has a LogRecord
with a message matching the specified matcher and with a Throwable matching the specified
matcher.
You must have first called capture(int).message - the matcher to match against LogRecord.getMessage()thrown - the matcher to match against LogRecord.getThrown(). Passing null is equivalent to
passing CoreMatchers.anything()public static org.hamcrest.Matcher<LoggerRule> recorded(@Nonnull org.hamcrest.Matcher<String> message)
Matcher that matches if the LoggerRule has a LogRecord
with a message matching the specified matcher.
You must have first called capture(int).message - the matcher to match against LogRecord.getMessage()Copyright © 2004–2019. All rights reserved.