Class LogListener
- java.lang.Object
-
- org.apache.solr.util.LogListener
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public final class LogListener extends Object implements Closeable, AutoCloseable
Helper code to listen forLogEvent
messages (via aQueue
) that you expect as a result of the things you are testing, So you can make assertions about when a particular action should/shouldn't cause Solr to produce a particular Log message// simplest possible usage... // Listen for any erors from the SolrCore logger, and assert that there are none... try (LogListener errLog = LogListener.error(SolrCore.class)) { // ... some test code ... assertEquals(0, errLog.getCount()); } // You can also use a substring or regex to restrict the messages that are considered, // and make assertions about the LogEvent's that match... try (LogListener secWarnLog = LogListener.warn("org.apache.solr.security").substring("PKI")) { // ... some test code ... // convinience method for only dealing with Message String of the LogEvent MatcherAssert.assertThat(secWarnLog.pollMessage(), containsString("hoss")); MatcherAssert.assertThat(secWarnLog.getQueue().isEmpty()); // no other WARNings matching PKI // ... more test code ... // More structured inspection of LogEvents... var logEvent = secWarnLog.getQueue().poll(); assertNotNull(logEvent); assertEquals("xyz", logEvent.getContextData().getValue("tid")); // check the MDC data }
Each
LogListener
captures & queues matching Log events until it isclose()
ed. By default the Queue is bounded at a max capacity of 100. Regardless of what Queue is used, if a Log event can't be queued (due to capacity limiting), or if events are still left in the Queue when the listener is closed, then theclose()
method will cause a test failure.Filtering methods such
substring(java.lang.String)
andregex(java.util.regex.Pattern)
can be used to restrict which Log events are recorded.Log messages are only recorded if they strictly match the Level specified, but the Logger "name" specified is matched hierarchically against any child Loggers (ie: You must know exactly what Log Level you are interested in, but you can capture all messages from the Loggers under a java package, or from inner classes of a single Logger)
NOTE: You Can not listen for ERROR messages from the root logger (
""
) If they are being "muted" byErrorLogMuter
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
static LogListener
debug()
Listens for DEBUG log messages at the ROOT loggerstatic LogListener
debug(Class<?> logger)
Listens for DEBUG log messages for the specified loggerstatic LogListener
debug(String logger)
Listens for DEBUG log messages for the specified loggerstatic LogListener
error()
Listens for ERROR log messages at the ROOT loggerstatic LogListener
error(Class<?> logger)
Listens for ERROR log messages for the specified loggerstatic LogListener
error(String logger)
Listens for ERROR log messages for the specified loggerint
getCount()
The total number of Log events so far processed by this instance, regardless of wether they have already been removed from the queue, or if they could not be added to the queue due to capacity restrictions.Queue<org.apache.logging.log4j.core.LogEvent>
getQueue()
Direct access to the Queue of Log events that have been recorded, forQueue.poll()
ing messages or any other inspection/manipulation.static LogListener
info()
Listens for INFO log messages at the ROOT loggerstatic LogListener
info(Class<?> logger)
Listens for INFO log messages for the specified loggerstatic LogListener
info(String logger)
Listens for INFO log messages for the specified loggerString
pollMessage()
Convinience method for tests that want to assert things about the (formated) message string at the head of the queue, w/o needing to know/call methods on the underlyingLogEvent
class.LogListener
regex(String regex)
Modifies this listener to filter the log events that are recorded to events that match the specified regex.LogListener
regex(Pattern pat)
Modifies this listener to filter the log events that are recorded to events that match the specified regex.LogListener
setQueue(Queue<org.apache.logging.log4j.core.LogEvent> queue)
Changes the queue that will be used to record any future events that matchLogListener
substring(String substr)
Modifies this listener to filter the log events that are recorded to events that match the specified substring.static LogListener
warn()
Listens for WARN log messages at the ROOT loggerstatic LogListener
warn(Class<?> logger)
Listens for WARN log messages for the specified loggerstatic LogListener
warn(String logger)
Listens for WARN log messages for the specified logger
-
-
-
Method Detail
-
error
public static LogListener error()
Listens for ERROR log messages at the ROOT logger
-
error
public static LogListener error(Class<?> logger)
Listens for ERROR log messages for the specified logger
-
error
public static LogListener error(String logger)
Listens for ERROR log messages for the specified logger
-
warn
public static LogListener warn()
Listens for WARN log messages at the ROOT logger
-
warn
public static LogListener warn(Class<?> logger)
Listens for WARN log messages for the specified logger
-
warn
public static LogListener warn(String logger)
Listens for WARN log messages for the specified logger
-
info
public static LogListener info()
Listens for INFO log messages at the ROOT logger
-
info
public static LogListener info(Class<?> logger)
Listens for INFO log messages for the specified logger
-
info
public static LogListener info(String logger)
Listens for INFO log messages for the specified logger
-
debug
public static LogListener debug()
Listens for DEBUG log messages at the ROOT logger
-
debug
public static LogListener debug(Class<?> logger)
Listens for DEBUG log messages for the specified logger
-
debug
public static LogListener debug(String logger)
Listens for DEBUG log messages for the specified logger
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
setQueue
public LogListener setQueue(Queue<org.apache.logging.log4j.core.LogEvent> queue)
Changes the queue that will be used to record any future events that match- See Also:
getQueue()
-
substring
public LogListener substring(String substr)
Modifies this listener to filter the log events that are recorded to events that match the specified substring.Log events are considered a match if their input matches either the message String, or the
toString
of an includedThrowable
, or any of the recursiveThrowable.getCause()
es of an includedThrowable
.At most one filtering method may be used
-
regex
public LogListener regex(Pattern pat)
Modifies this listener to filter the log events that are recorded to events that match the specified regex.Log events are considered a match if their input matches either the message String, or the
toString
of an includedThrowable
, or any of the recursiveThrowable.getCause()
es of an includedThrowable
.At most one filtering method may be used
-
regex
public LogListener regex(String regex)
Modifies this listener to filter the log events that are recorded to events that match the specified regex.Log events are considered a match if their input matches either the message String, or the
toString
of an includedThrowable
, or any of the recursiveThrowable.getCause()
es of an includedThrowable
.At most one filtering method may be used
-
getQueue
public Queue<org.apache.logging.log4j.core.LogEvent> getQueue()
Direct access to the Queue of Log events that have been recorded, forQueue.poll()
ing messages or any other inspection/manipulation.If a Log event is ever processed but can not be added to this queue (because
Queue.offer(E)
returns false) then theclose()
method of this listener will fail the test.
-
pollMessage
public String pollMessage()
Convinience method for tests that want to assert things about the (formated) message string at the head of the queue, w/o needing to know/call methods on the underlyingLogEvent
class.- Returns:
- the formatted message string of head of the queue, or null if the queue was empty when polled.
- See Also:
getQueue()
-
getCount
public int getCount()
The total number of Log events so far processed by this instance, regardless of wether they have already been removed from the queue, or if they could not be added to the queue due to capacity restrictions.
-
-