16

I've been trying to disable Velocity logs, and the only way I've found so far with positive results is to set:

runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogSystem

but inside the velocity.properties that resides inside the velocity.jar.

I'm using velocity in a Web Application (tomcat) context. Is there any way to disable the velocity (by setting the previous property or whatever) BUT without modifying the JAR??

Cannot modify any CODE

Thanks in advance

2
  • Post your application logging properties Oct 18, 2013 at 6:58
  • I just have a log4j.properties with nothing regarding velocity inside of it
    – luiso1979
    Oct 18, 2013 at 7:16

4 Answers 4

18

In general: just add following line to your velocity.properties :

runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogChute

To your question: It depends on how the velocity engine is loaded. Is it possible to give it a custom velocity.properties file?
For example Solr's VelocityResponseWriter has such property called v.properties (or init.properties.file in current versions).
Look, if the method org.apache.velocity.app.VelocityEngine.init(Properties) is called somewhere in the code...

2
  • Any idea how to do this from the command line, like using a system property? Doing something like java -Druntime.log.logsystem.class... doesn't seem to work.
    – santon
    May 10, 2018 at 22:57
  • I'm not 100% sure, but I think velocity itself is not reading system properties to update its configuration @santon
    – dedek
    May 14, 2018 at 12:50
6

This is how you can configure desired logging in velocity:

//java configuration
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute" );
ve.setProperty("runtime.log.logsystem.log4j.logger","velocity");

//log4j properties
log4j.category.velocity=WARN

IMHO INFO is fine with velocity as on startup you will notice some useful velocity engine configuration details, but during templating nothing specific comes out from INFO level.

2
  • cannot touch the code (sorry if I forgot that detail, I will edit the question)
    – luiso1979
    Oct 18, 2013 at 7:18
  • Also, I read the Velocity documentation
    – luiso1979
    Oct 18, 2013 at 7:20
3

You can implement the method VelocityEngine engine() of VelocityBuilder interface and then specify a file you want use as follow:

    public VelocityEngine engine() {
                if(engine == null) {
                    engine = new VelocityEngine();

                    Properties properties = new Properties();
                    InputStream in = null;
                    try {
//here is where you specify the filename "/WEB-INF/velocity.properties"
                        in = webApplicationContext.getServletContext().getResourceAsStream("/WEB-INF/velocity.properties");
                        properties.load(in);
                        engine.init(properties);
                    } catch (IOException e) {
                        e.printStackTrace();
                        logger.error("Error loading velocity engine properties");
                        throw new ProgramException("Cannot load velocity engine properties");
                    }

                    IOUtils.closeQuietly(in);
                }

                return engine;
            }

And after in your velocity.properties file:

resource.loader = framework

framework.resource.loader.description = Framework Templates Resource Loader
framework.resource.loader.class = applica.framework.library.velocity.WEBINFResourceLoader

webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path =
runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogSystem
file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path =

class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
velocityEngine.init();
1
  • 4
    Perhaps you could explain your answer, it would make it more valuable. May 31, 2018 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.