Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Error in velocity and log4J

I built a webapp that works perfectly fine in my localhost (tomcat). But when I tried to deploy, velocity crashes in init(), leaving me with this strange stack trace here (sorry for the size):


ERROR [main] (VelocityConfigurator.java:62) - Error initializing Velocity!
org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration.
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)
    at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)
    at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:589)
    at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:229)
    at org.apache.velocity.app.Velocity.init(Velocity.java:107)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.initVelocity(VelocityConfigurator.java:57)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.configure(VelocityConfigurator.java:42)
    at com.webcodei.velociraptor.VelociListener.contextInitialized(VelociListener.java:26)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.RuntimeException: Error configuring Log4JLogChute : 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.apache.velocity.util.ExceptionUtils.createWithCause(ExceptionUtils.java:67)
    at org.apache.velocity.util.ExceptionUtils.createRuntimeException(ExceptionUtils.java:45)
    at org.apache.velocity.runtime.log.Log4JLogChute.initAppender(Log4JLogChute.java:133)
    at org.apache.velocity.runtime.log.Log4JLogChute.init(Log4JLogChute.java:85)
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)
    ... 33 more
Caused by: java.io.FileNotFoundException: velocity.log (Permission denied)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.(FileOutputStream.java:177)
    at java.io.FileOutputStream.(FileOutputStream.java:102)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
    at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:194)
    at org.apache.log4j.FileAppender.(FileAppender.java:109)
    at org.apache.log4j.RollingFileAppender.(RollingFileAppender.java:72)
    at org.apache.velocity.runtime.log.Log4JLogChute.initAppender(Log4JLogChute.java:118)
    ... 35 more
ERROR [main] (VelocityConfigurator.java:63) - java.lang.RuntimeException: Error configuring Log4JLogChute : 
org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration.
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)
    at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)
    at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:589)
    at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:229)
    at org.apache.velocity.app.Velocity.init(Velocity.java:107)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.initVelocity(VelocityConfigurator.java:57)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.configure(VelocityConfigurator.java:42)
    at com.webcodei.velociraptor.VelociListener.contextInitialized(VelociListener.java:26)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.RuntimeException: Error configuring Log4JLogChute : 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.apache.velocity.util.ExceptionUtils.createWithCause(ExceptionUtils.java:67)
    at org.apache.velocity.util.ExceptionUtils.createRuntimeException(ExceptionUtils.java:45)
    at org.apache.velocity.runtime.log.Log4JLogChute.initAppender(Log4JLogChute.java:133)
    at org.apache.velocity.runtime.log.Log4JLogChute.init(Log4JLogChute.java:85)
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)
    ... 33 more
Caused by: java.io.FileNotFoundException: velocity.log (Permission denied)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.(FileOutputStream.java:177)
    at java.io.FileOutputStream.(FileOutputStream.java:102)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
    at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:194)
    at org.apache.log4j.FileAppender.(FileAppender.java:109)
    at org.apache.log4j.RollingFileAppender.(RollingFileAppender.java:72)
    at org.apache.velocity.runtime.log.Log4JLogChute.initAppender(Log4JLogChute.java:118)
    ... 35 more
ERROR [main] (VelocityConfigurator.java:64) - Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration.
org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration.
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)
    at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)
    at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:589)
    at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:229)
    at org.apache.velocity.app.Velocity.init(Velocity.java:107)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.initVelocity(VelocityConfigurator.java:57)
    at com.webcodei.velociraptor.velocity.VelocityConfigurator.configure(VelocityConfigurator.java:42)
    at com.webcodei.velociraptor.VelociListener.contextInitialized(VelociListener.java:26)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.ca

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Velocity tries to put the logfile in the directory Tomcat was started from, and I believe that is actually /.

You can force Velocity to log to Tomcat's standard log by adding these lines to your velocity.properties:

runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
runtime.log.logsystem.log4j.category=velocity
runtime.log.logsystem.log4j.logger=velocity

The velocity.properties should go into /WEB-INF/velocity.properties but you can override that in you servlet definition in web.xml.

If you're initialising Velocity by properties and not the velocity.properties:

VelocityEngine engine = new VelocityEngine();

Properties props = new Properties();
props.put("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
props.put("runtime.log.logsystem.log4j.category", "velocity");
props.put("runtime.log.logsystem.log4j.logger", "velocity");

engine.init(props);

Read more here:

http://velocity.apache.org/engine/devel/developer-guide.html#usinglog4jwithexistinglogger

and here:

http://minaret.biz/tips/tomcatLogging.html#velocity


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...