【问题标题】:How to disable velocity logs如何禁用速度日志
【发布时间】:2013-10-26 22:12:54
【问题描述】:

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

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

但在velocity.jar中的velocity.properties中。

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

不能修改任何代码

提前致谢

【问题讨论】:

  • 发布您的应用程序日志记录属性
  • 我只有一个 log4j.properties,里面没有任何关于速度的内容

标签: java tomcat logging velocity


【解决方案1】:

这是您可以在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

恕我直言INFOvelocity 很好,因为在启动时您会注意到一些有用的速度引擎配置细节,但在模板过程中没有任何具体来自INFO 级别。

【讨论】:

  • 无法触摸代码(对不起,如果我忘记了那个细节,我会编辑问题)
  • 另外,我阅读了 Velocity documentation
【解决方案2】:

您可以实现 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;
            }

然后在你的 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

【讨论】:

    【解决方案3】:

    一般而言:只需将以下行添加到您的 velocity.properties

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

    您的问题: 这取决于速度引擎的加载方式。是否可以给它一个自定义的velocity.properties 文件?
    例如,Solr 的VelocityResponseWriter 具有称为v.properties 的此类属性(或当前版本中的init.properties.file)。
    看,如果代码中某处调用了方法org.apache.velocity.app.VelocityEngine.init(Properties)...

    【讨论】:

    • 知道如何从命令行执行此操作,例如使用系统属性吗?像java -Druntime.log.logsystem.class... 这样的操作似乎不起作用。
    • 我不是 100% 确定,但我认为 Velocity 本身并没有读取系统属性来更新其配置@santon
    【解决方案4】:
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
    velocityEngine.init();
    

    【讨论】:

    • 也许你可以解释一下你的答案,这会让它更有价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多