Class LogRecorder

java.lang.Object
nva.commons.logutils.LogRecorder

public final class LogRecorder extends Object
Records log events emitted during a test. Unlike LogUtils, this helper does not reconfigure the Log4j context: it attaches a ListAppender to either a single class logger or the root logger, leaving the rest of the configuration untouched.
  • Method Details

    • forClass

      public static LogRecorder forClass(Class<?> clazz)
      Start a recorder on the logger for the given class. The returned recorder is empty: any previously captured events on the same logger are discarded. Tests that share a recorder across methods (for example, obtained once in @BeforeAll) should call clear() themselves between tests.
      Parameters:
      clazz - the class whose logger should be captured.
    • forRoot

      public static LogRecorder forRoot(Class<?> caller)
      Start a recorder on the root logger. The returned recorder is empty: any previously captured events on the root logger are discarded. The caller class is used to resolve the correct LoggerContext; pass the test class itself.
      Parameters:
      caller - the test class invoking this helper, used to resolve the right LoggerContext.
    • messages

      public List<String> messages()
      Formatted messages of all captured events, in the order they were emitted.
    • events

      public List<org.apache.logging.log4j.core.LogEvent> events()
      All captured log events, in the order they were emitted.
    • asString

      public String asString()
      Space-joined rendering of all captured messages. Thrown exception messages are appended.
    • clear

      public void clear()
      Discard all captured events.