Home > Spring, logback > Spring and logback glue

Spring and logback glue

I wanted dynamic configuration of my logback TurboFilter and i couldn’t find a good way to configure my logback TurboFilter through logback.xml. My requirement was to configure the filter based on a property from a properties file. I could have read the properties file in the filter class but i instead decided to use my Spring configuration for wiring in properties. To reap that benefit, the filter had to be a bean in the Spring context. Something like this when configured in XML

<bean id="turboFilter" class="com.nielsen.mmp.fw.logging.DynamicLoggerDisablingFilter">
    <property name="disabledLoggers" value="${disabledLoggers}"/>
</bean>

Now all i needed was some glue code that would make logback use this filter. Its been quite a while since i saw a need for a Spring BeanFactoryPostProcessor. I added a simple filter discovering post-processor that would discover all the TurboFilters defined in the Spring context and let logback know.

Map filterBeans = BeanFactoryUtils.
                            beansOfTypeIncludingAncestors(beanFactory,
                                                    TurboFilter.class);
    for (TurboFilter filter : filterBeans.values()) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	loggerContext.addTurboFilter(filter);
    }

Because we have hierarchical BeanFactories i had to use this BeanFactoryUtils.beansOfTypeIncludingAncestors(…) method. The usual BeanFactory.getBeansOfType() checks only in that beanFactory instance. It does not go through the parent beanFactories.

I wish Joran had a way (documented) to use values from properties file when configuring. Something like the following would have been neat.

    <configuration>
        <properties id="props" source="my-app.properties"/>
        <filter id="myTurboFilter" class="x.x.x.MyTurboFilter">
              <myFilterProperty>$props.propertyKey</myFilterProperty>
        </filter>
    </configuration>

Edit:
So i was mistaken. Joran does support properties file based customization just the way i had outlined. But, but, but, i don’t see it allowing property file overriding (ie a property file overriding the base property file) which is what we needed for our app. So my post processor is still going to be useful.

  1. No comments yet.
  1. No trackbacks yet.