Class RecordLogsHook

  • All Implemented Interfaces:
    GuiceyConfigurationHook

    public class RecordLogsHook
    extends java.lang.Object
    implements GuiceyConfigurationHook
    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): hook.record().start(Level.INFO) In most cases, it would be more convenient to listen to the exact logger logs: hook.record(Service.class).start(Level.INFO) - listen Service logs (assuming logger created as LoggerFactory.getLogger(Service.class)). Entire packages could be listened with: hook.register("com.package").start(Level.INFO). (class and string loggers could be specified together).

    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: RecordedLogs logs = hook.record().start(Level.INFO);. Raw recorded event objects could be used (logs.getEvents()) or just string messages (logs.getMessages()). There are many other methods to filter recorded logs.

    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 could be cleared either with RecordedLogs.clear() or with clearLogs() for all registered recorders.

    Since:
    30.04.2025