A Logback
Appender implementation which delegates the actual appending to a named bean contained in a Spring
ApplicationContext.
This appender is similar in spirit to Spring's
DelegatingFilterProxy, which allows servlet filters to be
created and wired in the ApplicationContext and then accessed in the filter chain. As with the filter proxy, the
delegating appender uses its own name to find the target appender in the context.
Because the logging framework is usually started before the Spring context, this appender supports caching for
ILoggingEvents which are received before the
ApplicationContext is available. This caching has
3 possible modes:
- off - Events are discarded until the
ApplicationContext is available.
- on - Events are cached with strong references until the
ApplicationContext is available, at
which time they will be forwarded to the delegate appender. In systems which produce substantial amounts of log
events while starting up the ApplicationContext this mode may result in heavy memory usage.
- soft - Events are wrapped in
SoftReferences and cached until the ApplicationContext
is available. Memory pressure may cause the garbage collector to collect some or all of the cached events before
the ApplicationContext is available, so some or all events may be lost. However, in systems with heavy
logging, this mode may result in more efficient memory usage.
Caching is
on by default, so strong references will be used for all events.
An example of how to use this appender in
logback.xml:
<appender name="appenderBeanName" class="ch.qos.logback.ext.spring.DelegatingLogbackAppender"/>
Or, if specifying a different cache mode, e.g.:
<appender name="appenderBeanName" class="ch.qos.logback.ext.spring.DelegatingLogbackAppender">
<cacheMode>soft</cacheMode>
</appender>
Using this appender requires that the
ApplicationContextHolder be included in the
ApplicationContext.