Class LogbackRecorder

  • 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 LogbackRecorder
    extends ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
    This class implements an 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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  LogbackRecorder.DenyFilter
      Simple deny filter.
    • Field Summary

      Fields 
      Modifier and Type Field 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).
      • Fields inherited from class ch.qos.logback.core.AppenderBase

        name, started
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected LogbackRecorder()
      Constructor only for use in unit tests.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      • 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 interface ch.qos.logback.core.spi.ContextAware

        addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
    • Field Detail

      • context

        protected static final ch.qos.logback.classic.LoggerContext context
        The Logback context currently in use.
      • root

        protected static final ch.qos.logback.classic.Logger root
        The root Logback logger, used to attach appender(s).
      • appender

        protected ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent> appender
        This instances appender.
      • events

        protected List<ch.qos.logback.classic.spi.ILoggingEvent> events
        List of archived logging events, can be reset any at point by calling reset().
    • Constructor Detail

      • LogbackRecorder

        protected LogbackRecorder()
        Constructor only for use in unit tests.
    • Method Detail

      • startRecorder

        public static LogbackRecorder startRecorder()
        Create a new LogbackRecorder and attach it to the current logging context's root logger.
      • stopRecorder

        public boolean stopRecorder()
        Stops recorder, clears recorded events and detaches appender from logging context's root logger.
        Returns:
        indication of success trying to detach appender
      • reset

        public void reset()
        Reset recorder by clearing all recorder events.
      • append

        protected void append​(ch.qos.logback.classic.spi.ILoggingEvent event)
        Specified by:
        append in class ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
      • isEmpty

        public boolean isEmpty()
        Returns boolean indicating whether any log entries have been recorded.
        Returns:
        boolean indicating whether any log entries have been recorded
      • assertLogContains

        public 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.
        Parameters:
        level - The log level of the log to find
        logStringToLookup - The string to find in the log.
      • assertLogContains

        public void assertLogContains​(String logStringToLookup)
        Assert that there is a recorded entry than contains the supplied string.
        Parameters:
        logStringToLookup - string to match for
      • assertLogContains

        public void assertLogContains​(String msg,
                                      String logStringToLookup)
        Assert that there is a recorded entry than contains the supplied string.
        Parameters:
        msg - error message or null
        logStringToLookup - string to match for
      • assertLogMatches

        public void assertLogMatches​(String msg,
                                     String regexToLookup)
        Assert that there is a recorded entry than matches the supplied regular expression.
        Parameters:
        msg - error message or null
        regexToLookup - regular expression to match for
      • assertLogNotContains

        public void assertLogNotContains​(String logStringToLookup)
        Assert that there is no recorded entry with the supplied string
        Parameters:
        logStringToLookup - log message to look for
      • assertLogNotContains

        public void assertLogNotContains​(String msg,
                                         String logStringToLookup)
        Assert that there is no recorded entry with the supplied string
        Parameters:
        msg - error message or null
        logStringToLookup - log message to look for
      • assertLogNotContainsLevel

        public void assertLogNotContainsLevel​(String msg,
                                              ch.qos.logback.classic.Level level)
        Assert that there is no recorded log entry with the supplied log level.
        Parameters:
        msg - error message or null
        level - log level
      • logIndexOf

        public int logIndexOf​(String logStringToLookup,
                              int fromIndex)
        Search the log entry list for a string starting from a specific index.
        Parameters:
        logStringToLookup - string to find
        fromIndex - log entry list start index
        Returns:
        index of next occurrence or -1, if not found
      • addFilter

        public 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.
        Parameters:
        filter - filter to add
        loggerName - name of logger
      • clearAllFilters

        public void clearAllFilters​(String loggerName)
        Remove all filters on all appenders registered with the logger with the supplied logger name.
        Parameters:
        loggerName - name of logger