Freemarker, slf4j and spring

I’ve just spent three hours trying to get Freemarker to stop spitting out “DEBUG cache:81” messages in my Spring application.

Freemarker recently hacked in SLF4J support into 2.3, but I had a hard time finding out how to enable it, so I reckoned I’d share my experiences.

FreeMarker 2.3 looks for logging libraries in this order (by default) with the class-loader of the FreeMarker classes: Log4J, Avalon, java.util.logging. The first that it founds in this list will be the one used for logging.

I found out that you can override this behavior in 2.3.18 by calling:


However, this code need to run before any Freemarker classes are initialized.

After trying a few different tricks, such as having a load-on-startup Servlet’s init() configure the logger, I ended up with a fairly clean solution.

I extended Spring’s FreeMarkerConfigurer class like this:

 public class PluxFreeMarkerConfigurer extends FreeMarkerConfigurer {
    private Logger logger = LoggerFactory

    public void afterPropertiesSet() throws IOException, TemplateException {

    private void fixFreemarkerLogging() {
        try {
  "Switched broken Freemarker logging to slf4j");
        } catch (ClassNotFoundException e) {
            logger.warn("Failed to switch broken Freemarker logging to slf4j");

and changed my Spring-config to use my class to initialize Freemarker instead:

  <!-- FreeMarker engine that configures Freemarker for SLF4J-->
  <bean id="freemarkerConfig" class="com.selessia.plux.web.PluxFreeMarkerConfigurer"

Hope this helps someone.