Class RecordLogsHook
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.log.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 asLoggerFactory.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
RecordedLogsobject: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 withclearLogs()for all registered recorders.- Since:
- 30.04.2025
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classRecordLogsHook.BuilderLog recorder configuration builder.classRecordLogsHook.RecordedLogsTrackingBundleTechnical bundle used to re-attach log recorders after dropwizard resets all loggers.
-
Constructor Summary
Constructors Constructor Description RecordLogsHook()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearLogs()Clear recorded logs for all registered recorders.voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).voiddestroy()Detach all registered appenders from logback loggers.RecordLogsHook.Builderrecord()Start recorder configuration.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
-
-
-
Method Detail
-
configure
public void configure(GuiceBundle.Builder builder) throws java.lang.Exception
Description copied from interface:GuiceyConfigurationHookConfiguration is applied just after manual configuration (through bundle's builder in application class).GuiceBundle.Buildercontains special methods for test support:- Generic disable:
GuiceBundle.Builder.disable(java.util.function.Predicate[]) - Direct disable* method, for example
GuiceBundle.Builder.disableExtensions(Class[]) - Guice bindings override:
GuiceBundle.Builder.modulesOverride(com.google.inject.Module...)
GuiceBundle.Builder.option(Enum, Object)).All configuration items, registered with hook will be scoped as
GuiceyConfigurationHookinstead ofApplicationand so will be clearly distinguishable in configuration logs (GuiceBundle.Builder.printDiagnosticInfo()).- Specified by:
configurein interfaceGuiceyConfigurationHook- Parameters:
builder- just created bundle's builder- Throws:
java.lang.Exception- on error (simplify usage)
- Generic disable:
-
record
public RecordLogsHook.Builder record()
Start recorder configuration. If no loggers provided then root logger would be listened (all events).Minimal usage:
record().loggers(Service.class).start(Level.INFO). Could be mixed with string-based loggers declaration:record().loggers(Service.class).loggers("some.string.logger").start(Level.INFO).- Returns:
- builder for additional configuration
-
clearLogs
public void clearLogs()
Clear recorded logs for all registered recorders.
-
destroy
public void destroy()
Detach all registered appenders from logback loggers.Not required as dropwizard reset all logging during application startup and so stale appenders would be removed in any case before each new test.
-
-