Annotation Type RecordLogs


  • @Retention(RUNTIME)
    @Target(FIELD)
    public @interface RecordLogs
    Record log events for verification. IMPORTANT: works ONLY with logback (would be useless with another logger).

    Without additional configuration would record all events (from the root logger): @RecordLogs RecordedLogs logs. In most cases, it would be more convenient to listen to the exact logger logs: @RecordLogs(Service.class) RecordedLogs logs - listen Service logs (assuming logger created as LoggerFactory.getLogger(Service.class)). Entire packages could be listened with: @RecordLogs(listeners = "com.package") RecordedLogs logs. (class and string loggers could be specified together).

    By default, listen WARN logs and above. To set a different level use @RecordLogs(value = Service.class level = Level.INFO) RecordedLogs logs. NOTE that logger level would be decreased (re-configured) to receive events from the required threshold.

    Could be used for a quick logger configuration changes in tests (easy switch to TRACE, for example).

    Recorded events could be inspected with RecordedLogs object: logs.getEvents() for raw event objects or logs.getMessages() for logged messages. There are many other methods to filter events.

    Events recorded for the entire application startup. Dropwizard resets loggers two times: in application constructor and just before the run phase (log configuration factory init), so logs listener appender have to be re-registered. LIMITATION: would not see run phase logs of dropwizard bundles, registered BEFORE GuiceBundle (no way re-attach listener before it). For dropwizard bundles, registered after guice bundle (or inside it) - all logs would be visible.

    Recorded logs are cleared after each test. Use autoReset() to disable. Also, clean could be performed manually with RecordedLogs.clear().

    Since:
    26.02.2025
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      boolean autoReset
      By default, recorded event reset after each test method.
      org.slf4j.event.Level level
      WARNING: if the current logger configuration is above the required threshold, then logger level would be updated!
      java.lang.String[] loggers
      Custom logger names, not based on class name.
      java.lang.Class<?>[] value
      Classes to track loggers for.
    • Element Detail

      • value

        java.lang.Class<?>[] value
        Classes to track loggers for. All log events would be recorded when empty.

        For string logger names use loggers() (could be used together with class loggers).

        Returns:
        logger classes to listen for
        Default:
        {}
      • loggers

        java.lang.String[] loggers
        Custom logger names, not based on class name. Useful for listening for entire packages.

        Works with class loggers (value()).

        Returns:
        string logger names to listen for
        Default:
        {}
      • level

        org.slf4j.event.Level level
        WARNING: if the current logger configuration is above the required threshold, then logger level would be updated! For example, if global logger level is set to WARN, but recorder level set to DEBUG then logger level would be reduced to receive all required events.
        Returns:
        required events threshold
        Default:
        org.slf4j.event.Level.WARN
      • autoReset

        boolean autoReset
        By default, recorded event reset after each test method. Use to disable automatic cleanup. Note that events could be cleared directly with RecordedLogs.clear().
        Returns:
        true to clean up recorded events after test
        Default:
        true