Suppress logging for Velocity

If you don’t want Velocity to log anything, you can change its default logging behavior. All you have to do is to implement an interface and set a property before you call Velocity.init().

Depending on the version of Velocity you’re using you’ll have to implement either LogSystem or LogChute.

Howto

If you’re using Velocity 1.4 or older use this class:

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.log.LogSystem;
 
public class VelocityNoOutputLogger implements LogSystem {
  public void init(RuntimeServices arg0) throws Exception {}
  public void logVelocityMessage(int arg0, String arg1) {}
}

If you’re using Velocity 1.5 or newer use this class:

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.log.LogChute;
 
public class VelocityNoOutputLogger implements LogChute {
  public void init(RuntimeServices arg0) throws Exception {}
  public boolean isLevelEnabled(int arg0) { return false; }
  public void log(int arg0, String arg1, Throwable arg2) {}
  public void log(int arg0, String arg1) {}
}

Finally you’ll have to tell Velocity about the new logger:

Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,
                     new VelocityNoOutputLogger());
Velocity.init();

Conclusion

We’ve implemented a simple way to suppress logging altogether. This comes in handy sometimes, so decide whether you want to use it too.