KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
20
21 import org.apache.log4j.Category;
22 import org.apache.log4j.RollingFileAppender;
23 import org.apache.log4j.PatternLayout;
24 import org.apache.log4j.Priority;
25 import org.apache.log4j.Appender;
26
27 import org.apache.velocity.runtime.RuntimeConstants;
28 import org.apache.velocity.runtime.RuntimeServices;
29
30 /**
31  * Implementation of a simple log4j system that will either
32  * latch onto an existing category, or just do a simple
33  * rolling file log. Derived from Jon's 'complicated'
34  * version :)
35  *
36  * @author <a HREF="mailto:geirm@apache.org>Geir Magnusson Jr.</a>
37  * @version $Id: SimpleLog4JLogSystem.java,v 1.1.8.1 2004/03/03 23:22:57 geirm Exp $
38  */

39 public class SimpleLog4JLogSystem implements LogSystem
40 {
41     private RuntimeServices rsvc = null;
42
43     /** log4java logging interface */
44     protected Category logger = null;
45
46     public SimpleLog4JLogSystem()
47     {
48     }
49
50     public void init( RuntimeServices rs )
51     {
52         rsvc = rs;
53
54         /*
55          * first see if there is a category specified and just use that - it allows
56          * the application to make us use an existing logger
57          */

58
59         String JavaDoc categoryname = (String JavaDoc) rsvc.getProperty("runtime.log.logsystem.log4j.category");
60
61         if ( categoryname != null )
62         {
63             logger = Category.getInstance( categoryname );
64         
65             logVelocityMessage( 0,
66                                 "SimpleLog4JLogSystem using category '" + categoryname + "'");
67
68             return;
69         }
70         
71         /*
72          * if not, use the file...
73          */

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

80         try
81         {
82             internalInit( logfile );
83
84             logVelocityMessage( 0,
85                 "SimpleLog4JLogSystem initialized using logfile '" + logfile + "'" );
86         }
87         catch( Exception JavaDoc e )
88         {
89             System.out.println(
90                 "PANIC : error configuring SimpleLog4JLogSystem : " + e );
91         }
92     }
93
94     /**
95      * initializes the log system using the logfile argument
96      */

97     private void internalInit( String JavaDoc logfile )
98         throws Exception JavaDoc
99     {
100         /*
101          * do it by our classname to avoid conflicting with anything else
102          * that might be used...
103          */

104
105         logger = Category.getInstance(this.getClass().getName());
106         logger.setAdditivity(false);
107
108         /*
109          * Priority is set for DEBUG becouse this implementation checks
110          * log level.
111          */

112         logger.setPriority(Priority.DEBUG);
113
114         RollingFileAppender appender = new RollingFileAppender( new PatternLayout( "%d - %m%n"), logfile, true);
115         
116         appender.setMaxBackupIndex( 1 );
117         
118         appender.setMaximumFileSize( 100000 );
119         
120         logger.addAppender(appender);
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         switch (level)
132         {
133             case LogSystem.WARN_ID:
134                 logger.warn( message );
135                 break;
136             case LogSystem.INFO_ID:
137                 logger.info(message);
138                 break;
139             case LogSystem.DEBUG_ID:
140                 logger.debug(message);
141                 break;
142             case LogSystem.ERROR_ID:
143                 logger.error(message);
144                 break;
145             default:
146                 logger.debug(message);
147                 break;
148         }
149     }
150
151     /**
152      * Also do a shutdown if the object is destroy()'d.
153      */

154     protected void finalize() throws Throwable JavaDoc
155     {
156         shutdown();
157     }
158
159     /** Close all destinations*/
160     public void shutdown()
161     {
162         Enumeration JavaDoc appenders = logger.getAllAppenders();
163         while (appenders.hasMoreElements())
164         {
165             Appender appender = (Appender)appenders.nextElement();
166             appender.close();
167         }
168     }
169 }
170
Popular Tags