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 forLogEventmessages (via aBlockingQueue) 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 assertThat(secWarnLog.pollMessage(), containsString("hoss")); 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
LogListenercaptures & 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 voidclearQueue()Clear the queue of any recorded eventsvoidclose()static LogListenerdebug()Listens for DEBUG log messages at the ROOT loggerstatic LogListenerdebug(Class<?> logger)Listens for DEBUG log messages for the specified loggerstatic LogListenerdebug(String logger)Listens for DEBUG log messages for the specified loggerstatic LogListenererror()Listens for ERROR log messages at the ROOT loggerstatic LogListenererror(Class<?> logger)Listens for ERROR log messages for the specified loggerstatic LogListenererror(String logger)Listens for ERROR log messages for the specified loggerintgetCount()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.BlockingQueue<org.apache.logging.log4j.core.LogEvent>getQueue()Direct access to the Queue of Log events that have been recorded, forBlockingQueue.poll(long, java.util.concurrent.TimeUnit)ing messages or any other inspection/manipulation.static LogListenerinfo()Listens for INFO log messages at the ROOT loggerstatic LogListenerinfo(Class<?> logger)Listens for INFO log messages for the specified loggerstatic LogListenerinfo(String logger)Listens for INFO log messages for the specified loggerStringpollMessage()Convenience 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 underlyingLogEventclass.StringpollMessage(long timeout, TimeUnit unit)Convenience method for tests that want to assert things about the (formated) message string at the head of the queue, waiting up to the specified timeout for the message to arrive.LogListenerregex(String regex)Modifies this listener to filter the log events that are recorded to events that match the specified regex.LogListenerregex(Pattern pat)Modifies this listener to filter the log events that are recorded to events that match the specified regex.LogListenersetQueue(BlockingQueue<org.apache.logging.log4j.core.LogEvent> queue)Changes the queue that will be used to record any future events that matchLogListenersubstring(String substr)Modifies this listener to filter the log events that are recorded to events that match the specified substring.static LogListenerwarn()Listens for WARN log messages at the ROOT loggerstatic LogListenerwarn(Class<?> logger)Listens for WARN log messages for the specified loggerstatic LogListenerwarn(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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
setQueue
public LogListener setQueue(BlockingQueue<org.apache.logging.log4j.core.LogEvent> queue)
Changes the queue that will be used to record any future events that match- See Also:
getQueue()
-
clearQueue
public void clearQueue()
Clear the queue of any recorded events
-
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
toStringof 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
toStringof 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
toStringof an includedThrowable, or any of the recursiveThrowable.getCause()es of an includedThrowable.At most one filtering method may be used
-
getQueue
public BlockingQueue<org.apache.logging.log4j.core.LogEvent> getQueue()
Direct access to the Queue of Log events that have been recorded, forBlockingQueue.poll(long, java.util.concurrent.TimeUnit)ing messages or any other inspection/manipulation.If a Log event is ever processed but can not be added to this queue (because
BlockingQueue.offer(E)returns false) then theclose()method of this listener will fail the test.
-
pollMessage
public String pollMessage()
Convenience 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 underlyingLogEventclass.- Returns:
- the formatted message string of head of the queue, or null if the queue was empty when polled.
- See Also:
getQueue()
-
pollMessage
public String pollMessage(long timeout, TimeUnit unit)
Convenience method for tests that want to assert things about the (formated) message string at the head of the queue, waiting up to the specified timeout for the message to arrive.- Parameters:
timeout- the duation valueunit- the duration unit- Returns:
- the formatted message string of head of the queue, or null if the queue remained empty until the specified timeout.
-
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.
-
-