Java 如何禁用速度日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19443366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:12:21  来源:igfitidea点击:

How to disable velocity logs

javatomcatloggingvelocity

提问by luiso1979

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

我一直在尝试禁用 Velocity 日志,到目前为止我发现的唯一具有积极结果的方法是设置:

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

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

但在位于velocity.jar 内的velocity.properties 内。

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??

我在 Web 应用程序 (tomcat) 上下文中使用速度。有没有办法禁用速度(通过设置以前的属性或其他)但不修改 JAR?

Cannot modify any CODE

不能修改任何代码

Thanks in advance

提前致谢

回答by harsh

This is how you can configure desired logging in velocity:

这是您可以配置所需登录的方法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 INFOis fine with velocityas on startup you will notice some useful velocity engine configuration details, but during templating nothing specific comes out from INFOlevel.

恕我直言,INFO是用细velocity如在启动时,你会发现一些有用的极速引擎配置的详细信息,但在没有任何模板化从具体出来INFO的水平。

回答by Giuseppe Iacobucci

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

您可以实现 VelocityBuilder 接口的 VelocityEngine engine() 方法,然后指定您要使用的文件,如下所示:

    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:

然后在您的velocity.properties 文件中:

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

回答by dedek

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

一般来说:只需将以下行添加到您的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.propertiesfile?
For example Solr's VelocityResponseWriterhas such property called v.properties(or init.properties.filein current versions).
Look, if the method org.apache.velocity.app.VelocityEngine.init(Properties)is called somewhere in the code...

对于您的问题:这取决于速度引擎的加载方式。可以给它一个自定义velocity.properties文件吗?
例如 SolrVelocityResponseWriter有这样的属性调用v.properties(或init.properties.file在当前版本中)。
看,如果该方法org.apache.velocity.app.VelocityEngine.init(Properties)在代码中的某处被调用......

回答by Oleg Mikhailov

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