Code
1package velocity;
2import log4j.Log4jDemo;
3import org.apache.log4j.BasicConfigurator;
4import org.apache.log4j.Logger;
5import org.apache.log4j.PropertyConfigurator;
6import org.apache.velocity.app.VelocityEngine;
7import org.apache.velocity.runtime.RuntimeConstants;
8public class VelocityLogDemo {
9 public static String LOGGER_NAME = "velexample";
10 public static void main(String[] args) throws Exception {
11 PropertyConfigurator.configure("src/main/java/velocity/log4j.properties ");
12 //BasicConfigurator.configure();
13 Logger log = Logger.getLogger( LOGGER_NAME );
14 log.info("Log4jLoggerExample: ready to start velocity");
15 VelocityEngine ve = new VelocityEngine();
16 ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
17 "org.apache.velocity.runtime.log.Log4JLogChute" );
18 ve.setProperty("runtime.log.logsystem.log4j.logger",
19 LOGGER_NAME);
20 ve.init();
21 //Velocity.init("src/main/java/velocity/velocity.properties");
22 }
23}
最重要的地方就在于log4j的logger Name,如果没有这个log,那么velocity就无法找到打印的target。1package velocity;
2import log4j.Log4jDemo;
3import org.apache.log4j.BasicConfigurator;
4import org.apache.log4j.Logger;
5import org.apache.log4j.PropertyConfigurator;
6import org.apache.velocity.app.VelocityEngine;
7import org.apache.velocity.runtime.RuntimeConstants;
8public class VelocityLogDemo {
9 public static String LOGGER_NAME = "velexample";
10 public static void main(String[] args) throws Exception {
11 PropertyConfigurator.configure("src/main/java/velocity/log4j.properties ");
12 //BasicConfigurator.configure();
13 Logger log = Logger.getLogger( LOGGER_NAME );
14 log.info("Log4jLoggerExample: ready to start velocity");
15 VelocityEngine ve = new VelocityEngine();
16 ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
17 "org.apache.velocity.runtime.log.Log4JLogChute" );
18 ve.setProperty("runtime.log.logsystem.log4j.logger",
19 LOGGER_NAME);
20 ve.init();
21 //Velocity.init("src/main/java/velocity/velocity.properties");
22 }
23}