Class InMemoryLogAppender
java.lang.Object
ch.qos.logback.core.spi.ContextAwareBase
ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
io.github.cjstehno.testthings.slf4j.InMemoryLogAppender
- All Implemented Interfaces:
ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent>
,ch.qos.logback.core.spi.ContextAware
,ch.qos.logback.core.spi.FilterAttachable<ch.qos.logback.classic.spi.ILoggingEvent>
,ch.qos.logback.core.spi.LifeCycle
public class InMemoryLogAppender
extends ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
An
Appender
for the SLF4J logging library (with logback backend) that allows for verifying
results that may only appear in log messages.
To use this appender you register the classes to be monitored using the loggedClass(Class)
configuration
method and then call the attach()
method on the appender to hook it into the logging system.
You should ensure that the detach()
method is also called when you are done testing so that the test code
is not left in the logger configuration.
Various methods are provided to read through and filter the captured log messages.
See also the LogAppenderExtension
for a simple means of using this in tests.
-
Field Summary
Fields inherited from class ch.qos.logback.core.AppenderBase
name, started
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
Constructor Summary
ConstructorDescriptionInMemoryLogAppender
(AppenderConfig config) Creates a log appender with the provided configuration object.InMemoryLogAppender
(Consumer<AppenderConfig> consumer) Creates an in-memory LogbackAppender
for testing with the provided configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
append
(ch.qos.logback.classic.spi.ILoggingEvent event) Called when a logging event is to be added to the appender.void
attach()
Must be called to initiate the log event recording - to register the log appender with the logger.int
count()
Counts all captured logging events.int
count
(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Counts the number of captured events matching the provided matcher.void
detach()
Should be called when done using the appender to clean up resources.List<ch.qos.logback.classic.spi.ILoggingEvent>
events()
Retrieves all captured logging events.List<ch.qos.logback.classic.spi.ILoggingEvent>
events
(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Retrieves all captured logging events matching the provided matcher.boolean
hasEvent
(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Returnstrue
if there is at least one captured logging event matching the provided matcher.Stream<ch.qos.logback.classic.spi.ILoggingEvent>
stream()
Retrieves the captured logging events as a Stream.Stream<ch.qos.logback.classic.spi.ILoggingEvent>
stream
(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Retrieves the captured logging events matching the provided matcher, as a Stream.Methods inherited from class ch.qos.logback.core.AppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, start, stop, toString
Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
Constructor Details
-
InMemoryLogAppender
Creates an in-memory LogbackAppender
for testing with the provided configuration.- Parameters:
consumer
- the configuration
-
InMemoryLogAppender
Creates a log appender with the provided configuration object.- Parameters:
config
- the configuration object
-
-
Method Details
-
attach
public void attach()Must be called to initiate the log event recording - to register the log appender with the logger. -
detach
public void detach()Should be called when done using the appender to clean up resources. -
append
protected void append(ch.qos.logback.classic.spi.ILoggingEvent event) Called when a logging event is to be added to the appender.- Specified by:
append
in classch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
- Parameters:
event
- the logging event
-
count
public int count(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Counts the number of captured events matching the provided matcher.- Parameters:
matcher
- the event matcher- Returns:
- the count of matching events
-
count
public int count()Counts all captured logging events.- Returns:
- the count
-
events
public List<ch.qos.logback.classic.spi.ILoggingEvent> events(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Retrieves all captured logging events matching the provided matcher.- Parameters:
matcher
- the matcher- Returns:
- the list of matching events
-
events
Retrieves all captured logging events.- Returns:
- all captured logging events
-
hasEvent
public boolean hasEvent(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Returnstrue
if there is at least one captured logging event matching the provided matcher.- Parameters:
matcher
- the matcher- Returns:
- true if there is a matching event
-
stream
public Stream<ch.qos.logback.classic.spi.ILoggingEvent> stream(org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> matcher) Retrieves the captured logging events matching the provided matcher, as a Stream.- Parameters:
matcher
- the matcher- Returns:
- the stream of matching events
-
stream
Retrieves the captured logging events as a Stream.- Returns:
- the stream of events
-