Changing Location of Velocity.Log File

后端 未结 1 1989
时光取名叫无心
时光取名叫无心 2021-01-12 13:27

Seems pretty straight forward. Documentation at http://velocity.apache.org/engine/devel/developer-guide.html#Configuring_Logging says to set the runtime.log property. Here\'

相关标签:
1条回答
  • 2021-01-12 14:15

    i had similar problem when setting at runtime some options. I figured out those problem whit a custom VelocityBuilder and an external velocity.properties file where you can put all the runtime properties. Here is the code:

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

    See this line:

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

    So i have a velocity.properties file in /WEB-INF where i put some configuration:

        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'
    

    In the end in your application.xml :

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

    In this way you can have for example different file log for different application and when you give the war in production, the sysadm can change the properties due to env configuration of the production server.

    0 讨论(0)
提交回复
热议问题