2011-06-02 34 views
5

似乎很简单。文档http://velocity.apache.org/engine/devel/developer-guide.html#Configuring_Logging 表示设置runtime.log属性。这就是我的所有财产。更改Velocity.Log文件的位置

velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatesPath); 

      velocityEngine.setProperty("runtime.log", "/path/to/my/file/velocity.log"); 
      velocityEngine.setProperty("resource.loader", "string"); 
      velocityEngine.setProperty("string.resource.loader.class", "org.apache.velocity.runtime.resource.loader.StringResourceLoader"); 
      velocityEngine.setProperty("string.resource.loader.repository.class", "org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl"); 

没有发现任何日志文件,其中我告诉它把它转而寻找放置到原来的位置(初始化文件夹)的新的错误。有任何想法吗? :D

+0

这对我来说很合适。该位置是否具有正确的权限?作为替代方案,您还可以使用log4j并将其合并到常规应用程序日志中。 – 2011-06-12 20:41:19

回答

1

我在运行时设置一些选项时遇到了类似的问题。我想出了一个自定义VelocityBuilder和外部velocity.properties文件的问题,您可以在其中放置所有运行时属性。 下面是代码:

public class BaseVelocityBuilder implements VelocityBuilder { 
    private VelocityEngine engine; 

    private Log logger = LogFactory.getLog(getClass()); 

    @Autowired 
    private WebApplicationContext webApplicationContext; 

    public VelocityEngine engine() { 
     if(engine == null) { 
      engine = new VelocityEngine(); 

      Properties properties = new Properties(); 
      InputStream in = null; 
      try { 
       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; 
    } 
} 

看到这一行:

  in = webApplicationContext.getServletContext().getResourceAsStream("/WEB-INF/velocity.properties"); 
      properties.load(in); 
      engine.init(properties); 

所以我在/ WEB-INF一个velocity.properties文件,其中我把一些配置:

resource.loader = webinf, class 

webinf.resource.loader.description = Framework Templates Resource Loader 
webinf.resource.loader.class = applica.framework.library.velocity.WEBINFResourceLoader 

webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader 
webapp.resource.loader.path = 

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 
runtime.log='/pathYouWant/velocity.log' 

最后在application.xml中:

<bean class="applica.framework.library.velocity.BaseVelocityBuilder" /> 

通过这种方式,您可以为不同的应用程序创建不同的文件日志,并且当您在生产中进行战争时,由于生产服务器的env配置,sysadm可以更改属性。