How to disable velocity logs

2020-08-09 10:48发布

问题:

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

回答1:

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:

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.



回答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


回答4:

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
velocityEngine.init();