速度从 1.7 升级到 2.0 [英] Velocity upgrade from 1.7 to 2.0

查看:94
本文介绍了速度从 1.7 升级到 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用 LogChute 界面的速度 1.7 迁移.在我当前的实现中,我使用 log 方法来获取速度日志级别并比较我们自己的日志级别.请看下面的代码.

I am trying to migrate from velocity 1.7 where I use LogChute interface. In my current implementation I have used the log method to get the velocity log level and comparing our own log level. Please see the code below.

@Override
public void log(int level, String message) {
    LogLevel projLevel = null;
    switch ( level )
    {
       case LogChute.DEBUG_ID:
          projLevel = LogLevel.DEBUG ;
          break ;

       case LogChute.INFO_ID:
          projLevel = LogLevel.INFO ;
          break ;

       case LogChute.WARN_ID:
          projLevel = LogLevel.WARNING ;
          break ;

       case LogChute.ERROR_ID:
          projLevel = LogLevel.ERROR ;
          break ;

       default:
          projLevel = LogLevel.ERROR ;
          break ;
    }

    if (Log.canLog(projLevel, Const.VELOCITY_LOGGER))
    {
       Log.log(projLevel, Const.VELOCITY_LOGGER, getClass(), message,
          null);
    }

}

基于 apache 速度 2.0 文档,不推荐使用 LogChute,而 apache 速度使用 SLF4J 进行日志记录.因此,我尝试将 SLF4j-API 和 SLF4J 绑定用作 SLF4J Simple Logger 和 WebApp SLF4J Logger,但无法利用该类,因为我需要将速度日志级别与我们的自定义日志级别进行比较.所有这些都需要在运行时发生.

Based on apache velocity 2.0 documentation the LogChute is deprecated and the apache velocity is using SLF4J for logging. So, I tried to use SLF4j-API and SLF4J bindings as SLF4J Simple Logger and WebApp SLF4J Logger but unable to utilize the class as I need to compare the velocity log levels with our custom log levels. All these needs to happened during runtime.

对于当前速度配置,我遵循以下配置,这与基于 1.7 速度配置调用的自定义类相同,为 services.VelocityService.runtime.log.logsystem.class=our.package.xclassName.

For current velocity configuration, I am following the below configuration this is same as custom class which is invoked based on 1.7 velocity configuration as services.VelocityService.runtime.log.logsystem.class=our.package.xclassName.

这是文档的链接(https://velocity.apache.org/engine/1.7/developer-guide.html#configuring-logging)

这些都在 2.0 版本中删除了.

These all are removed in 2.0 version.

有人可以帮我解决这个问题吗?我正在尝试升级速度.

Can someone help me on this. I am trying to upgrade the velocity.

推荐答案

基本上,您希望能够动态设置日志级别.也许您应该将 slf4j 绑定从 slf4j-api 更改为 logback,请参阅 这个问题.

Basically, you want to be able to dynamically set the log level. Maybe you should change your slf4j binding from slf4j-api to logback, see this question.

如果你想坚持 slf4j-simple,也许你可以尝试给 Velocity 一个自定义的 slf4j 记录器实例(未测试):

If you want to stick to slf4j-simple, maybe you can try giving Velocity a custom slf4j logger instance (not tested):

public class MyCustomLogger extends org.slf4j.SimpleLogger
{
    public MyCustomLogegr(String name) { super(name); }

    protected boolean isLevelEnabled(int logLevel)
    {
        LogLevel projLevel = null;
        switch ( level )
        {
           case LogChute.DEBUG_ID:
              projLevel = LogLevel.DEBUG ;
              break ;
           case LogChute.INFO_ID:
              projLevel = LogLevel.INFO ;
              break ;
           case LogChute.WARN_ID:
              projLevel = LogLevel.WARNING ;
              break ;
           case LogChute.ERROR_ID:
              projLevel = LogLevel.ERROR ;
              break ;
           default:
              projLevel = LogLevel.ERROR ;
              break ;
        }
        return Log.canLog(projLevel, Const.VELOCITY_LOGGER);
    }
}

然后,您必须将此记录器提供给 Velocity:

Then, you must give this logger to Velocity:

VelocityEngine velocity = new VelocityEngine();
// ... other configurations
velocity.setProperty("runtime.log.instance", new MyCustomLogger());
velocity.initialize();

这篇关于速度从 1.7 升级到 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆