Class RecordLogsHook
- All Implemented Interfaces:
GuiceyConfigurationHook
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassLog recorder configuration builder.final classTechnical bundle used to re-attach log recorders after dropwizard resets all loggers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClear 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.record()Start recorder configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
Constructor Details
-
RecordLogsHook
public RecordLogsHook()
-
-
Method Details
-
configure
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:
Exception- on error (simplify usage)
- Generic disable:
-
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.
-