KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > LoggingManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 /******************************** Package */
7 package com.raptus.owxv3;
8
9 /******************************** Imports */
10 import java.util.Hashtable JavaDoc;
11
12 /******************************** Raptus-Header */
13 /**
14  * This is the LoggingManager of OWX which is used for logging through the entire
15  * OWX. In the same manner as ConfigManager, this uses classes which implement
16  * ConfigManagerIFace as logger-objects. Changing the used implementation is done
17  * as in ConfigManager by giving the name of the instance you wish through the
18  * servletparams-hashtable to the initialize-method. In addition to ConfigManager,
19  * this uses a default if no log-manager was specified. The default is specified
20  * in a constant below.
21  *
22  * Configuration-keys begin with PARAM_PREFIX.
23  *
24  * @see com.raptus.owxv3.ConfigManager ConfigManager
25  * @see com.raptus.owxv3.LoggingManagerIFace LoggingManagerIFace
26  *
27  *
28  * <hr>
29  * <table width="100%" border="0">
30  * <tr>
31  * <td width="24%"><b>Filename</b></td><td width="76%">LoggingManager.java</td>
32  * </tr>
33  * <tr>
34  * <td width="24%"><b>Author</b></td><td width="76%">Pascal Mainini (pmainini@raptus.com)</td>
35  * </tr>
36  * <tr>
37  * <td width="24%"><b>Date</b></td><td width="76%">3rd of April 2001</td>
38  * </tr>
39  * </table>
40  * <hr>
41  * <table width="100%" border="0">
42  * <tr>
43  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
44  * </tr>
45  * <tr>
46  * <td width="24%">2001-04-03/pm</td><td width="76%">created</td>
47  * </tr>
48  * </table>
49  * <hr>
50  */

51 public class LoggingManager extends Object JavaDoc
52 {
53
54 /******************************** Constants */
55     /**
56      * This constant specifies the prefix which identifies a key in the
57      * servlet-params-hash belonging to LoggingManager-initial-config.
58      *
59      * <b>Change this only if you know exactly why!</b>
60      */

61     public static final String JavaDoc PARAM_PREFIX = "LoggingManager.";
62
63     /**
64      * This specifies a concrete implementation of LogginManagerIFace which will
65      * be used for logging.
66      */

67     public static final String JavaDoc HANDLER_KEY = "LogHandler";
68
69     /**
70      * This specifes which key to use when retrieving the verbosity-level.
71      */

72     public static final String JavaDoc VERBOSITY_KEY = "Verbosity";
73
74     /**
75      * This is the default-logger if no other is specified
76      */

77     public static final String JavaDoc DEFAULT_HANDLER = "com.raptus.owxv3.TomcatLoggingManager";
78
79
80 /******************************** Verbosity-levels */
81     /**
82      * Nothing is logged.
83      */

84     public static final int VERBOSITY_NOLOGS = 0;
85
86     /**
87      * Info, normal, critical and fatal messages are logged (excluding debug-messages).
88      */

89     public static final int VERBOSITY_ALL = 80;
90
91     /**
92      * Normal, critical and fatal messages are logged (excluding debug-messages).
93      */

94     public static final int VERBOSITY_NORMAL = 60;
95
96     /**
97      * Only debug-messages are logged.
98      */

99     public static final int VERBOSITY_DEBUG = 150;
100
101     /**
102      * Absolutely everything is logged
103      */

104     public static final int VERBOSITY_DEBUG_ALL = 160;
105
106
107 /******************************** Importance-levels */
108     /**
109      * This is a normal log-message. Could be an information
110      * like "everything works fine now".
111      */

112     public static final int IMPORTANCE_INFO = 5;
113
114     /**
115      * This is a normal log-message. Could be an information
116      * like "everything works fine now".
117      */

118     public static final int IMPORTANCE_NORMAL = 10;
119
120     /**
121      * This log-message speciefies critical occurences like
122      * "intrusion detected".
123      */

124     public static final int IMPORTANCE_CRITICAL = 20;
125
126     /**
127      * This is a very sad logmessage like "System killed"
128      */

129     public static final int IMPORTANCE_FATAL = 30;
130
131     /**
132      * This are messages which are only used when developing/debugging
133      */

134     public static final int IMPORTANCE_DEBUG = 100;
135
136
137 /******************************** Members */
138     /**
139      * A singleton instance (autmatically created)
140      */

141     protected static LoggingManager _instance = null;
142
143     /**
144      * This hashtable stores all information needed to configure
145      * this class and it's instantiated managers. The startup-servlet
146      * needs to pass it to the constructor.
147      */

148     protected Hashtable JavaDoc myServletParams = null;
149
150     /**
151      * Instance of the handler used to access config. Is specified
152      * with the hashkey HANDLER_KEY.
153      */

154     protected LoggingManagerIFace myLogger = null;
155
156     /**
157      * This is the level which we log.
158      */

159     protected static int verbosityLevel = VERBOSITY_NORMAL;
160
161     /**
162      * This hashtable stores the names for all the levels.
163      */

164     protected static Hashtable JavaDoc levelNames;
165
166
167 /******************************** Constructors */
168     /**
169      * This constructor is only called from getInstance(Hashtable params),
170      * it sets up the servlet-params-hashtable.
171      *
172      * @param params All servletparams needed, in a hashtable
173      */

174     private LoggingManager(Hashtable JavaDoc params)
175     {
176         this.myServletParams = params;
177     }
178
179
180 /******************************** Methods */
181     /**
182      * This allows to reset the Verbosity-level
183      * without reinitializing the entire thing.
184      *
185      * @param level Verbositylevel to set
186      * @return true iv level was valid,false if not,
187      */

188     public static boolean setVerbosity(int level)
189     {
190         if(!isValidVerbosity(level))
191             return false;
192
193         verbosityLevel=level;
194         return true;
195     }
196
197     /**
198      * A simple getter for the actual verbosity-level.
199      *
200      * @return The actual level
201      */

202     public static int getVerbosity()
203     {
204         return verbosityLevel;
205     }
206
207     /**
208      * the singleton mechanism
209      *
210      * @return the only instance of this class, or null if it hasn't been instantiated yet.
211      */

212     public static LoggingManager getInstance()
213     {
214         return _instance;
215     }
216
217     /**
218      * get access to the configured logger object. (used internally)
219      */

220     public LoggingManagerIFace getLogger()
221     {
222         return myLogger;
223     }
224
225     /**
226      * this is a special adapted version of the singleton-mechanism only used
227      * by the startup-servlet. If there's actually no instance of this class,
228      * then it's constructed with the params-hashtable.
229      *
230      * For the very first time, the class <b>MUSST</b> be instantiated this way,
231      * because each call of getInstance() before calling this will end up in returning
232      * null.
233      *
234      * @param params The hashtable containing the servlet-params.
235      * @return the only instance of this class, or null if initializing failed.
236      */

237     public static LoggingManager getInstance(Hashtable JavaDoc params)
238     {
239         if(_instance == null)
240             _instance = new LoggingManager(params);
241
242         if(!_instance.initialize())
243             return null;
244
245         return _instance;
246     }
247
248
249     /**
250      * This method is called only by getInstance(Hashtable hash) in order to set
251      * up important stuff.
252      *
253      * @return true if all init-stuff was successful, false if not.
254      */

255     protected boolean initialize()
256     {
257         // Load the appropriate ConfigHandler depending on handlerClass.
258
String JavaDoc handlerClass = null;
259         if(myServletParams != null)
260             handlerClass = (String JavaDoc) myServletParams.get(PARAM_PREFIX + HANDLER_KEY);
261
262         if(handlerClass == null || handlerClass.length() == 0)
263             handlerClass = DEFAULT_HANDLER;
264
265         try
266         {
267             Class JavaDoc classObj = Class.forName(handlerClass);
268             if(classObj == null)
269                 return false;
270
271
272             myLogger = (LoggingManagerIFace) classObj.newInstance();
273                 if(myLogger == null)
274                     return false;
275
276         }
277         catch(Exception JavaDoc e)
278         {
279             // if the loggingmanager fails to create we can't log,
280
// so just return false...
281

282             return false;
283         }
284
285         if(myLogger.initialize(myServletParams) == false)
286             return false;
287
288         // Try to figure out which loglevel we should use or set it to normal if not specifed
289
String JavaDoc verbosity = (String JavaDoc) myServletParams.get(PARAM_PREFIX + VERBOSITY_KEY);
290         if(verbosity != null && verbosity.length() > 0)
291         {
292             try
293             {
294                 verbosityLevel = new Integer JavaDoc(verbosity).intValue();
295             }
296             catch(NumberFormatException JavaDoc e)
297             {
298                 verbosityLevel = VERBOSITY_NORMAL;
299             }
300
301             if(!isValidVerbosity(verbosityLevel))
302                 verbosityLevel = VERBOSITY_NORMAL;
303         }
304
305         // Setup names for the levels
306
levelNames = new Hashtable JavaDoc();
307         levelNames.put(new Integer JavaDoc(IMPORTANCE_INFO), "INFO");
308         levelNames.put(new Integer JavaDoc(IMPORTANCE_NORMAL), "NORMAL");
309         levelNames.put(new Integer JavaDoc(IMPORTANCE_CRITICAL), "CRITICAL");
310         levelNames.put(new Integer JavaDoc(IMPORTANCE_FATAL), "FATAL");
311         levelNames.put(new Integer JavaDoc(IMPORTANCE_DEBUG), "DEBUG");
312         return true;
313     }
314
315 /******************************** Forwarding-methods */
316     /**
317      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg) Definition
318      */

319     public static void log(String JavaDoc msg)
320     {
321         //System.out.println(msg);
322
LoggingManager lm = LoggingManager.getInstance();
323         lm.getLogger().log(msg);
324     }
325
326     /**
327      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, int level) Definition
328      */

329     public static void log(String JavaDoc msg, int level)
330     {
331         //LoggingManager.log(msg);
332
LoggingManager lm = LoggingManager.getInstance();
333         lm.getLogger().log(msg, level);
334     }
335
336     /**
337      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, Object caller) Definition
338      */

339     public static void log(String JavaDoc msg, Object JavaDoc caller)
340     {
341         //LoggingManager.log(caller.getClass().getName()+" "+msg);
342
LoggingManager lm = LoggingManager.getInstance();
343         lm.getLogger().log(msg, caller);
344     }
345
346     /**
347      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, Object caller, int level) Definition
348      */

349     public static void log(String JavaDoc msg, Object JavaDoc caller, int level)
350     {
351         //LoggingManager.log(caller.getClass().getName()+" "+msg);
352
LoggingManager lm = LoggingManager.getInstance();
353         lm.getLogger().log(msg, caller, level);
354     }
355
356 /******************************** Helper-methods */
357     /**
358      * @param level The level to which the name should belong.
359      * @return a String which represents the verbosity-level.
360      */

361     public static String JavaDoc getBelongingLevelName(int level)
362     {
363         return (String JavaDoc) levelNames.get(new Integer JavaDoc(level));
364     }
365
366     /**
367      * This method checks, if the specified level is to log actuallly or not.
368      *
369      * @param level The level to check
370      * @return true if the level is to log, false if not.
371      */

372     public static boolean isToLog(int level)
373     {
374         if(verbosityLevel == VERBOSITY_DEBUG_ALL)
375             return true;
376
377         else if(verbosityLevel == VERBOSITY_NOLOGS)
378             return false;
379
380         else if(verbosityLevel == VERBOSITY_DEBUG && level == IMPORTANCE_DEBUG)
381             return true;
382
383         else if(verbosityLevel == VERBOSITY_ALL && level < VERBOSITY_ALL)
384             return true;
385
386         else if(verbosityLevel == VERBOSITY_NORMAL && level < VERBOSITY_ALL && level >= IMPORTANCE_NORMAL)
387             return true;
388
389         return false;
390     }
391
392     /**
393      * This method checks if the specified level is a valid level
394      * Actually, no check is performed but can be implemented anytime.
395      * @param level the level to check
396      * @return true if the specified level is valid, false if not.
397      */

398     public static boolean isValidVerbosity(int level)
399     {
400         return true;
401     }
402 }
403
Popular Tags