Class LogListener

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public final class LogListener
    extends Object
    implements Closeable, AutoCloseable
    Helper code to listen for LogEvent messages (via a Queue) 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 LogListener captures & queues matching Log events until it is close()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 the close() method will cause a test failure.

    Filtering methods such substring(java.lang.String) and regex(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" by ErrorLogMuter.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      static LogListener error()
      Listens for ERROR log messages at the ROOT logger
      static LogListener error​(Class<?> logger)
      Listens for ERROR log messages for the specified logger
      static LogListener error​(String logger)
      Listens for ERROR log messages for the specified logger
      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.
      Queue<org.apache.logging.log4j.core.LogEvent> getQueue()
      Direct access to the Queue of Log events that have been recorded, for Queue.poll()ing messages or any other inspection/manipulation.
      static LogListener info()
      Listens for INFO log messages at the ROOT logger
      static LogListener info​(Class<?> logger)
      Listens for INFO log messages for the specified logger
      static LogListener info​(String logger)
      Listens for INFO log messages for the specified logger
      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 underlying LogEvent 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 match
      LogListener 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 logger
      static LogListener warn​(Class<?> logger)
      Listens for WARN log messages for the specified logger
      static 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
      • 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 included Throwable, or any of the recursive Throwable.getCause()es of an included Throwable.

        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 included Throwable, or any of the recursive Throwable.getCause()es of an included Throwable.

        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 included Throwable, or any of the recursive Throwable.getCause()es of an included Throwable.

        At most one filtering method may be used

      • 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 underlying LogEvent 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.