Class MockLogAppender

java.lang.Object
org.apache.logging.log4j.core.AbstractLifeCycle
org.apache.logging.log4j.core.filter.AbstractFilterable
org.apache.logging.log4j.core.appender.AbstractAppender
org.opensearch.test.MockLogAppender
All Implemented Interfaces:
AutoCloseable, org.apache.logging.log4j.core.Appender, org.apache.logging.log4j.core.filter.Filterable, org.apache.logging.log4j.core.impl.LocationAware, org.apache.logging.log4j.core.LifeCycle, org.apache.logging.log4j.core.LifeCycle2

public class MockLogAppender extends org.apache.logging.log4j.core.appender.AbstractAppender implements AutoCloseable
Test appender that can be used to verify that certain events were logged correctly
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static class 
     
    static interface 
     
    static class 
     
    static class 
    Used for cases when the logger is dynamically named such as to include an index name or shard id Best used in conjunction with the root logger: @TestLogging(value = "_root:debug", reason = "Validate logging output");
    static class 
     
    static class 
     

    Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B extends org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B>>

    Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle

    org.apache.logging.log4j.core.LifeCycle.State
  • Field Summary

    Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER

    Fields inherited from interface org.apache.logging.log4j.core.Appender

    ELEMENT_TYPE, EMPTY_ARRAY
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    append(org.apache.logging.log4j.core.LogEvent event)
     
    void
     
    void
     
    createForLoggers(String filter, org.apache.logging.log4j.Logger... loggers)
     
    createForLoggers(org.apache.logging.log4j.Logger... loggers)
    Creates an instance and adds it as an appender to the given Loggers.
    void
     

    Methods inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    error, error, error, getHandler, getLayout, getName, ignoreExceptions, parseInt, requiresLocation, setHandler, toSerializable, toString

    Methods inherited from class org.apache.logging.log4j.core.filter.AbstractFilterable

    addFilter, getFilter, getPropertyArray, hasFilter, isFiltered, removeFilter, start, stop, stop

    Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.apache.logging.log4j.core.LifeCycle

    getState, initialize, isStarted, isStopped, start
  • Method Details

    • createForLoggers

      public static MockLogAppender createForLoggers(org.apache.logging.log4j.Logger... loggers) throws IllegalAccessException
      Creates an instance and adds it as an appender to the given Loggers. Upon closure, this instance will then remove itself from the Loggers it was added to. It is strongly recommended to use this class in a try-with-resources block to guarantee that it is properly removed from all Loggers. Since the logging state is static and therefore global within a JVM, it can cause unrelated tests to fail if, for example, they trigger a logging statement that tried to write to a closed MockLogAppender instance.
      Throws:
      IllegalAccessException
    • createForLoggers

      public static MockLogAppender createForLoggers(String filter, org.apache.logging.log4j.Logger... loggers) throws IllegalAccessException
      Throws:
      IllegalAccessException
    • addExpectation

      public void addExpectation(MockLogAppender.LoggingExpectation expectation)
    • append

      public void append(org.apache.logging.log4j.core.LogEvent event)
      Specified by:
      append in interface org.apache.logging.log4j.core.Appender
    • assertAllExpectationsMatched

      public void assertAllExpectationsMatched()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • stop

      public void stop()
      Specified by:
      stop in interface org.apache.logging.log4j.core.LifeCycle
      Overrides:
      stop in class org.apache.logging.log4j.core.AbstractLifeCycle