Velocity upgrade from 1.7 to 2.0

半腔热情 提交于 2021-02-08 06:38:33

问题


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);
    }

}

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.

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.

Here's the link for documentation(https://velocity.apache.org/engine/1.7/developer-guide.html#configuring-logging)

These all are removed in 2.0 version.

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


回答1:


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.

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);
    }
}

Then, you must give this logger to Velocity:

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


来源:https://stackoverflow.com/questions/65673539/velocity-upgrade-from-1-7-to-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!