更改 Velocity.Log 文件的位置

Change the Location of Velocity.Log File

我用的是 velocity-1.6。2.jar

我的问题 velocity.log 是在服务器上创建的 jboss/bin

jboss/bin 有只读权限。

在我的应用程序中,当 velocity-1.6.2.jar 被调用时,我遇到了这个错误:

Caused by: java.io.FileNotFoundException: velocity.log (Permission denied)

我想更改 Velocity.Log 文件的位置

这是我的速度属性中的定位线:

# ----------------------------------------------------------------------------
#  default LogChute to use: default: AvalonLogChute, Log4JLogChute, CommonsLogLogChute, ServletLogChute, JdkLogChute
# ----------------------------------------------------------------------------

runtime.log.logsystem.class = org.apache.velocity.runtime.log.AvalonLogChute,org.apache.velocity.runtime.log.Log4JLogChute,org.apache.velocity.runtime.log.CommonsLogLogChute,org.apache.velocity.runtime.log.ServletLogChute,org.apache.velocity.runtime.log.JdkLogChute

# ---------------------------------------------------------------------------
# This is the location of the Velocity Runtime log.
# ----------------------------------------------------------------------------

runtime.log = velocity.log

Runtime log 可以获得完整路径,因此只需将日志作为 /home/velocity.log

直接登录到您想要的文件夹
 runtime.log = /home/velocity.log

Full path and name of log file for error, warning, and informational messages. The location, if not absolute, is relative to the 'current directory'.

更新 (27-02-2018)

日志路径也可以通过编程方式设置如下:

import org.apache.velocity.app.VelocityEngine;
public class VelocityCustomEngine extends TemplateEngine {

    private final VelocityEngine velocityEngine;

    public VelocityCustomEngine() {
        Properties properties = new Properties();
        properties.setProperty("runtime.log", "/home/velocity.log");

        this.velocityEngine = new VelocityEngine(properties);
    }
}

我知道有答案。如果您不想扩展原始的 VelocityEngine。 您还可以新建一个 VelocityEngine 实例,仅在 init() 方法之前更改 "runtime.log" 属性,而无需任何新的 class.

        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty("runtime.log", "/home/velocity.log");
        velocityEngine.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.init();//init method will use the properties(include the log file location) actually.
        return velocityEngine;