public class LogbackRecorder extends ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
Logback
appender which can be attached dynamically to an
SLF4J
context. The appender stores logging events in memory so their occurrence (or lack of) can be
validated, most likely, in unit test.
It can be used to test whether logging is performed. The normal usage is:
public void testSomething() {
LogbackRecorder logRecorder = LogbackRecorder.startRecorder();
doTheTesting();
logRecorder.assertLogContains(theStringToVerifyIsInTheLog);
}
Remember to call the stopRecorder method on the logRecorder instance when finished. The probability
of doing this on a consistent basis is increased if the LogbackRecorder.startRecorder() and stopRecorder calls
are made as part of the @Before and @After test methods.Modifier and Type | Class and Description |
---|---|
static class |
LogbackRecorder.DenyFilter
Simple deny filter.
|
Modifier and Type | Field and Description |
---|---|
protected ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent> |
appender
This instances appender.
|
protected static ch.qos.logback.classic.LoggerContext |
context
The Logback context currently in use.
|
protected List<ch.qos.logback.classic.spi.ILoggingEvent> |
events
List of archived logging events, can be reset any at point by calling reset().
|
protected static ch.qos.logback.classic.Logger |
root
The root Logback logger, used to attach appender(s).
|
Modifier | Constructor and Description |
---|---|
protected |
LogbackRecorder()
Constructor only for use in unit tests.
|
Modifier and Type | Method and Description |
---|---|
void |
addFilter(ch.qos.logback.core.filter.Filter<ch.qos.logback.classic.spi.ILoggingEvent> filter,
String loggerName)
Add filter on all appenders registered with the logger with the supplied logger name.
|
protected void |
append(ch.qos.logback.classic.spi.ILoggingEvent event) |
void |
assertLogContains(ch.qos.logback.classic.Level level,
String logStringToLookup)
Tries to find a log entry with a specific log level containing a specific string and fails the if no match is
found.
|
void |
assertLogContains(String logStringToLookup)
Assert that there is a recorded entry than contains the supplied string.
|
void |
assertLogContains(String msg,
String logStringToLookup)
Assert that there is a recorded entry than contains the supplied string.
|
void |
assertLogMatches(String msg,
String regexToLookup)
Assert that there is a recorded entry than matches the supplied regular expression.
|
void |
assertLogNotContains(String logStringToLookup)
Assert that there is no recorded entry with the supplied string
|
void |
assertLogNotContains(String msg,
String logStringToLookup)
Assert that there is no recorded entry with the supplied string
|
void |
assertLogNotContainsLevel(String msg,
ch.qos.logback.classic.Level level)
Assert that there is no recorded log entry with the supplied log level.
|
void |
clearAllFilters(String loggerName)
Remove all filters on all appenders registered with the logger with the supplied logger name.
|
boolean |
isEmpty()
Returns boolean indicating whether any log entries have been recorded.
|
int |
logIndexOf(String logStringToLookup,
int fromIndex)
Search the log entry list for a string starting from a specific index.
|
void |
reset()
Reset recorder by clearing all recorder events.
|
static LogbackRecorder |
startRecorder()
Create a new
LogbackRecorder and attach it to the current logging context's root logger. |
boolean |
stopRecorder()
Stops recorder, clears recorded events and detaches appender from logging context's root logger.
|
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, start, stop, toString
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
protected static final ch.qos.logback.classic.LoggerContext context
protected static final ch.qos.logback.classic.Logger root
protected ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent> appender
protected LogbackRecorder()
public static LogbackRecorder startRecorder()
LogbackRecorder
and attach it to the current logging context's root logger.public boolean stopRecorder()
public void reset()
protected void append(ch.qos.logback.classic.spi.ILoggingEvent event)
append
in class ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
public boolean isEmpty()
public void assertLogContains(ch.qos.logback.classic.Level level, String logStringToLookup)
level
- The log level of the log to findlogStringToLookup
- The string to find in the log.public void assertLogContains(String logStringToLookup)
logStringToLookup
- string to match forpublic void assertLogContains(String msg, String logStringToLookup)
msg
- error message or nulllogStringToLookup
- string to match forpublic void assertLogMatches(String msg, String regexToLookup)
msg
- error message or nullregexToLookup
- regular expression to match forpublic void assertLogNotContains(String logStringToLookup)
logStringToLookup
- log message to look forpublic void assertLogNotContains(String msg, String logStringToLookup)
msg
- error message or nulllogStringToLookup
- log message to look forpublic void assertLogNotContainsLevel(String msg, ch.qos.logback.classic.Level level)
msg
- error message or nulllevel
- log levelpublic int logIndexOf(String logStringToLookup, int fromIndex)
logStringToLookup
- string to findfromIndex
- log entry list start indexpublic void addFilter(ch.qos.logback.core.filter.Filter<ch.qos.logback.classic.spi.ILoggingEvent> filter, String loggerName)
filter
- filter to addloggerName
- name of loggerpublic void clearAllFilters(String loggerName)
loggerName
- name of loggerCopyright © 2005–2015 The Royal Danish Library, the Danish State and University Library, the National Library of France and the Austrian National Library.. All rights reserved.