1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.webapp.logging.velocity;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.velocity.runtime.RuntimeServices;
22 import org.apache.velocity.runtime.log.LogSystem;
23
24 /***
25 * Implementation of a LogSystem using Commons Logging to route Velocity message
26 * through a IsolatedLog4JLogger setup.
27 * <p>
28 * Configure the following in your velocity.properties:
29 * <ul>
30 * <li>runtime.log.logsystem.class=org.apache.jetspeed.webapp.logging.velocity.CommonsLoggingLog4JLogSystem</li>
31 * <li>runtime.log.logsystem.log4j.category=<a Log4J Category name to capture Velocity message, default value: "velocity"></li>
32 * </ul>
33 * For further information about setting up and configuring velocity:
34 * <a href="http://jakarta.apache.org/velocity/docs/developer-guide.html">Velocity - Developer's Guide</a>
35 * </p>
36 * <p>
37 * If you want to use a VelocityEngine instantiated by Spring using its org.springframework.ui.velocity.VelocityEngineFactoryBean
38 * then you can also configure the above properties inline in its defintion or point it to your velocity.properties file.<br/>
39 * But, beware of the following: the VelocityEngineFactoryBean by default overrides logging any configuration and hooks up their own
40 * CommonsLoggingLogSystem. Which works fine just as this one, but uses as (hard coded) logging category the VelocityEngine class name.
41 * So, if you do want to route your Velocity logging using your own category (or our default "velocity"), then you need to override the
42 * VelocityEngineFactoryBean default logging setup by setting its "overrideLogging" property to false.
43 * </p>
44 * <p>
45 * </p>
46 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
47 * @version $Id$
48 */
49 public class CommonsLoggingLog4JLogSystem implements LogSystem
50 {
51 public static final String DEFAULT_CATEGORY = "velocity";
52
53 private Log logger;
54
55
56
57
58 public void init(RuntimeServices rs) throws Exception
59 {
60 String categoryname = (String) rs.getProperty("runtime.log.logsystem.log4j.category");
61
62 if ( categoryname == null )
63 {
64 categoryname = DEFAULT_CATEGORY;
65 }
66 logger = LogFactory.getLog(categoryname);
67
68 logVelocityMessage( DEBUG_ID, "CommonsLoggingLog4JLogSystem using category '" + categoryname + "'");
69 }
70
71
72
73
74 public void logVelocityMessage(int level, String message)
75 {
76 switch (level)
77 {
78 case LogSystem.WARN_ID:
79 logger.warn( message );
80 break;
81 case LogSystem.INFO_ID:
82 logger.info(message);
83 break;
84 case LogSystem.DEBUG_ID:
85 logger.debug(message);
86 break;
87 case LogSystem.ERROR_ID:
88 logger.error(message);
89 break;
90 default:
91 logger.debug(message);
92 break;
93 }
94 }
95 }