KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LoggerExample


1 /*
2  * Copyright 2000-2001,2004 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 import org.apache.velocity.app.Velocity;
18 import org.apache.velocity.runtime.log.LogSystem;
19 import org.apache.velocity.runtime.RuntimeServices;
20
21
22 /**
23  * This is a toy demonstration of how Velocity
24  * can use an externally configured logger. In
25  * this example, the class using Velocity
26  * implements Velocity's logger interface, and
27  * all Velocity log messages are funneled back
28  * through it.
29  *
30  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
31  * @version $Id: LoggerExample.java,v 1.2.10.1 2004/03/04 00:18:29 geirm Exp $
32  */

33 public class LoggerExample implements LogSystem
34 {
35     public LoggerExample()
36     {
37         try
38         {
39             /*
40              * this class implements the LogSystem interface, so we
41              * can use it as a logger for Velocity
42              */

43             
44             Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this );
45             Velocity.init();
46
47             /*
48              * that will be enough. The Velocity initialization will be
49              * output to stdout because of our
50              * logVelocityMessage() method in this class
51              */

52         }
53         catch( Exception JavaDoc e )
54         {
55             System.out.println("Exception : " + e);
56         }
57     }
58
59     /**
60      * Required init() method for LogSystem
61      * to get access to RuntimeServices
62      */

63      public void init( RuntimeServices rs )
64      {
65         return;
66      }
67      
68     /**
69      * This is the key method needed to implement a logging interface
70      * for Velocity.
71      */

72     public void logVelocityMessage(int level, String JavaDoc message)
73     {
74         System.out.println("level : " + level + " msg : " + message);
75     }
76
77     public static void main(String JavaDoc[] args)
78     {
79         LoggerExample t = new LoggerExample();
80     }
81 }
82
Popular Tags