KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > generic > log > CommonsLogLogSystem


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.generic.log;
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  * Redirects Velocity's LogSystem messages to commons-logging.
26  *
27  * <p>To use, first set up commons-logging, then tell Velocity to use
28  * this class for logging by adding the following to your velocity.properties:
29  *
30  * <code>
31  * runtime.log.logsystem.class = org.apache.velocity.tools.generic.log.CommonsLogLogSystem
32  * </code>
33  * </p>
34  *
35  * <p>You may also set this property to specify what log/name Velocity's
36  * messages should be logged to (example below is default).
37  * <code>
38  * runtime.log.logsystem.commons.logging.name = org.apache.velocity
39  * </code>
40  * </p>
41  *
42  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
43  * @since VelocityTools 1.1
44  * @version $Id: CommonsLogLogSystem.java,v 1.3 2004/02/18 20:10:38 nbubna Exp $
45  */

46 public class CommonsLogLogSystem implements LogSystem
47 {
48
49     /** Property key for specifying the name for the log instance */
50     public static final String JavaDoc LOGSYSTEM_COMMONS_LOG_NAME =
51         "runtime.log.logsystem.commons.logging.name";
52
53     /** Default name for the commons-logging instance */
54     public static final String JavaDoc DEFAULT_LOG_NAME = "org.apache.velocity";
55
56     
57     /** the commons-logging Log instance */
58     protected Log log;
59
60
61     /********** LogSystem methods *************/
62
63     public void init(RuntimeServices rs) throws Exception JavaDoc
64     {
65         String JavaDoc name =
66             (String JavaDoc)rs.getProperty(LOGSYSTEM_COMMONS_LOG_NAME);
67         
68         if (name == null)
69         {
70             name = DEFAULT_LOG_NAME;
71         }
72         log = LogFactory.getLog(name);
73         logVelocityMessage(LogSystem.DEBUG_ID,
74                            "CommonsLogLogSystem name is '" + name + "'");
75     }
76
77     /**
78      * Send a log message from Velocity.
79      */

80     public void logVelocityMessage(int level, String JavaDoc message)
81     {
82         switch (level)
83         {
84             case LogSystem.WARN_ID:
85                 log.warn(message);
86                 break;
87             case LogSystem.INFO_ID:
88                 log.info(message);
89                 break;
90             case LogSystem.DEBUG_ID:
91                 log.debug(message);
92                 break;
93             case LogSystem.ERROR_ID:
94                 log.error(message);
95                 break;
96             default:
97                 log.debug(message);
98                 break;
99         }
100     }
101
102 }
103
Popular Tags