KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > log > AvalonLogSystem


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

18
19 import java.io.File JavaDoc;
20
21 import org.apache.log.Priority;
22 import org.apache.log.Logger;
23 import org.apache.log.Hierarchy;
24 import org.apache.log.LogTarget;
25 import org.apache.log.output.io.FileTarget;
26
27 import org.apache.velocity.runtime.RuntimeServices;
28 import org.apache.velocity.runtime.RuntimeConstants;
29
30 /**
31  * Implementation of a Avalon logger.
32  *
33  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
34  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
35  * @version $Id: AvalonLogSystem.java,v 1.12.4.1 2004/03/03 23:22:56 geirm Exp $
36  */

37 public class AvalonLogSystem implements LogSystem
38 {
39     private Logger logger = null;
40
41     private RuntimeServices rsvc = null;
42
43     /**
44      * default CTOR. Initializes itself using the property RUNTIME_LOG
45      * from the Velocity properties
46      */

47
48     public AvalonLogSystem()
49     {
50     }
51
52     public void init( RuntimeServices rs )
53         throws Exception JavaDoc
54     {
55         this.rsvc = rs;
56
57         /*
58          * if a logger is specified, we will use this instead of
59          * the default
60          */

61         String JavaDoc loggerName = (String JavaDoc) rsvc.getProperty("runtime.log.logsystem.avalon.logger");
62         
63         if (loggerName != null)
64         {
65             this.logger = Hierarchy.getDefaultHierarchy().getLoggerFor(loggerName);
66         }
67         else
68         {
69             /*
70              * since this is a Velocity-provided logger, we will
71              * use the Runtime configuration
72              */

73             String JavaDoc logfile = (String JavaDoc) rsvc.getProperty( RuntimeConstants.RUNTIME_LOG );
74
75             /*
76              * now init. If we can't, panic!
77              */

78             try
79             {
80                 init( logfile );
81
82                 logVelocityMessage( 0,
83                     "AvalonLogSystem initialized using logfile '" + logfile + "'" );
84             }
85             catch( Exception JavaDoc e )
86             {
87                 System.out.println(
88                     "PANIC : Error configuring AvalonLogSystem : " + e );
89                 System.err.println(
90                     "PANIC : Error configuring AvalonLogSystem : " + e );
91
92                 throw new Exception JavaDoc("Unable to configure AvalonLogSystem : " + e );
93             }
94         }
95     }
96
97     /**
98      * initializes the log system using the logfile argument
99      *
100      * @param logFile file for log messages
101      */

102     public void init(String JavaDoc logFile)
103         throws Exception JavaDoc
104     {
105
106     /*
107      * make our FileTarget. Note we are going to keep the
108      * default behavior of not appending...
109      */

110         FileTarget target = new FileTarget( new File JavaDoc( logFile),
111                         false,
112                         new VelocityFormatter("%{time} %{message}\\n%{throwable}" ) );
113        
114         /*
115          * use the toString() of RuntimeServices to make a unique logger
116          */

117
118         logger = Hierarchy.getDefaultHierarchy().getLoggerFor( rsvc.toString() );
119         logger.setPriority( Priority.DEBUG );
120         logger.setLogTargets( new LogTarget[] { target } );
121     }
122     
123     /**
124      * logs messages
125      *
126      * @param level severity level
127      * @param message complete error message
128      */

129     public void logVelocityMessage(int level, String JavaDoc message)
130     {
131         /*
132          * based on level, call teh right logger method
133          * and prefix with the appropos prefix
134          */

135
136         switch (level)
137         {
138             case LogSystem.WARN_ID:
139                 logger.warn( RuntimeConstants.WARN_PREFIX + message );
140                 break;
141             case LogSystem.INFO_ID:
142                 logger.info( RuntimeConstants.INFO_PREFIX + message);
143                 break;
144             case LogSystem.DEBUG_ID:
145                 logger.debug( RuntimeConstants.DEBUG_PREFIX + message);
146                 break;
147             case LogSystem.ERROR_ID:
148                 logger.error(RuntimeConstants.ERROR_PREFIX + message);
149                 break;
150             default:
151                 logger.info( message);
152                 break;
153         }
154     }
155 }
156
Popular Tags