KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > logging > Log


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /*
66  *
67  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
68  */

69
70 package com.jcorporate.expresso.core.logging;
71
72 import com.jcorporate.expresso.core.db.DBConnection;
73 import com.jcorporate.expresso.core.db.DBConnectionPool;
74 import com.jcorporate.expresso.core.db.DBException;
75 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
76 import com.jcorporate.expresso.services.dbobj.LogEntry;
77
78 import javax.servlet.ServletContext JavaDoc;
79 import java.io.ByteArrayOutputStream JavaDoc;
80 import java.io.IOException JavaDoc;
81 import java.io.PrintStream JavaDoc;
82
83
84 /**
85  * Log is a generic logging facility that determines what logging mechanisms
86  * are available to it & logs a message to those mechanisms.
87  * The potential mechanisms are:
88  * An Database log, viewable as a web page
89  * A servlet log, location dependant on servlet engine
90  * Standard output (if all else fails!)
91  *
92  * @author Michael Nash
93  * @since Expresso 1.0
94  * @deprecated Since Expresso 4.0
95  */

96 public class Log {
97     private static ServletContext JavaDoc myContext = null;
98     private static String JavaDoc thisClass = ("com.jcorporate.expresso." +
99             "core.logging.Log.");
100     private static boolean consoleOutput = false;
101     private static int maxLevel = 9; /* log everything by default */
102     private static DBConnectionPool myPool = null;
103
104     /**
105      * Constructor
106      */

107     public Log() {
108     } /* Log() */
109
110     /**
111      * Verify that the log is ready to log messages, return false if not
112      * able to be initialized
113      *
114      * @return true
115      * @throws LogException if fails checkLog();
116      */

117     private static boolean checkLog()
118             throws LogException {
119         String JavaDoc myName = (thisClass + "checkLog()");
120
121         try {
122             if (myPool != null) {
123                 return true;
124             }
125
126             myPool = DBConnectionPool.getInstance("default");
127             setMax(9);
128
129             return true;
130         } catch (DBException de) {
131             throw new LogException(myName + ":Unable to read setup values:" +
132                     de.getMessage());
133         }
134
135     } /* checkLog() */
136
137
138     /**
139      * Delete all current database log entries
140      *
141      * @return String: Filename of the copy made of the existing log
142      * @throws IOException If the file cannot be copied
143      * @throws DBException If the setup info cannot be read
144      */

145     public static String JavaDoc clear()
146             throws IOException JavaDoc, DBException {
147         String JavaDoc myName = (thisClass + "clear()");
148         DBConnection myConnection = null;
149
150         try {
151             if (checkLog()) {
152                 myConnection = myPool.getConnection(myName);
153
154                 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
155                 myConnection.executeUpdate("DELETE FROM " +
156                         myLog.getJDBCMetaData().getTargetSQLTable(myLog.getDataContext()));
157             } else {
158                 throw new DBException(myName +
159                         ":Unable to clear log, logging " +
160                         "not initialized");
161             }
162         } catch (LogException le) {
163             throw new DBException(le.getMessage());
164         } finally {
165             if (myPool != null) {
166                 myPool.release(myConnection);
167             }
168         }
169
170         return ("Log Cleared");
171     } /* clear() */
172
173
174     /**
175      * Translate a color spelt out in english in to a one-char encoding
176      *
177      * @param color
178      * @return
179      */

180     private static String JavaDoc encode(String JavaDoc color) {
181         if (color.equalsIgnoreCase("RED")) {
182             return ("R");
183         } else if (color.equalsIgnoreCase("GREEN")) {
184             return ("G");
185         } else if (color.equalsIgnoreCase("BLUE")) {
186             return ("B");
187         } else if (color.equalsIgnoreCase("YELLOW")) {
188             return ("Y");
189         } else {
190             return ("");
191         }
192     } /* encode(String) */
193
194     /**
195      * Log a new message at the given level, if we log at or above this level
196      *
197      * @param newLevel Log level
198      * @param msg Message to log
199      */

200     public static void log(int newLevel, String JavaDoc msg)
201             throws LogException {
202         String JavaDoc myName = (thisClass + "log(int, String)");
203
204         if (newLevel <= maxLevel) {
205             if (consoleOutput) {
206                 System.err.println(msg);
207             }
208
209             DBConnection myConnection = null;
210
211             if (checkLog()) {
212                 try {
213                     myConnection = myPool.getConnection(myName);
214
215                     LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
216                     myLog.setField("MessageText", msg);
217                     myLog.setField("MessageLevel", "" + newLevel);
218                     myLog.add();
219                 } catch (DBException de) {
220                     System.err.println(myName + ":Unable to log '" + msg +
221                             "'");
222                     de.printStackTrace(System.err);
223                 } finally {
224                     if (myPool != null) {
225                         myPool.release(myConnection);
226                     }
227                 }
228             } /* if */
229
230             if (myContext != null) {
231                 myContext.log(msg);
232             }
233         } /* if level is logged */
234
235     } /* log(int, String) */
236
237
238     /**
239      * Log the given message at the given level, recording the originating
240      * object
241      *
242      * @param newLevel Logging level of this message
243      * @param objectName Calling object
244      * @param msg Message to log
245      */

246     public static void log(int newLevel, String JavaDoc objectName, String JavaDoc msg)
247             throws LogException {
248         String JavaDoc myName = (thisClass + "log(int, String, String)");
249
250         if (newLevel <= maxLevel) {
251             if (consoleOutput) {
252                 System.err.println(objectName + ":" + msg);
253             }
254
255             DBConnection myConnection = null;
256
257             if (checkLog()) {
258                 try {
259                     myConnection = myPool.getConnection(myName);
260
261                     LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
262                     myLog.setField("ObjectName", objectName);
263                     myLog.setField("MessageText", msg);
264                     myLog.setField("MessageLevel", "" + newLevel);
265                     myLog.add();
266                 } catch (DBException de) {
267                     throw new LogException(myName + ":" + de.getMessage());
268                 } finally {
269                     if (myPool != null) {
270                         myPool.release(myConnection);
271                     }
272                 }
273             }
274             if (myContext != null) {
275                 myContext.log(objectName + "|" + msg);
276             }
277         } /* if this level is logged */
278
279     } /* log(int, String, String) */
280
281
282     /**
283      * Log a message from a particular object with a color at the given
284      * level
285      *
286      * @param newLevel Message level to log
287      * @param objectName Calling object
288      * @param msg Message to log
289      * @param color Color to log the message with
290      * @throws LogException if the message cannot be logged
291      */

292     public static void log(int newLevel, String JavaDoc objectName, String JavaDoc msg,
293                            String JavaDoc color)
294             throws LogException {
295         String JavaDoc myName = (thisClass + "log(int, String, String, " +
296                 "String)");
297
298         if (newLevel <= maxLevel) {
299             if (consoleOutput) {
300                 System.err.println(objectName + ":" + msg);
301             }
302
303             DBConnection myConnection = null;
304
305             if (checkLog()) {
306                 try {
307                     myConnection = myPool.getConnection(myName);
308
309                     LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
310                     myLog.setField("ObjectName", objectName);
311                     myLog.setField("MessageText", msg);
312                     myLog.setField("MessageColor", encode(color));
313                     myLog.setField("MessageLevel", "" + newLevel);
314                     myLog.add();
315                 } catch (DBException de) {
316                     System.err.println(myName + ":Unable to log '" + msg +
317                             "'");
318                     de.printStackTrace(System.err);
319                 } finally {
320                     if (myPool != null) {
321                         myPool.release(myConnection);
322                     }
323                 }
324             }
325             if (myContext != null) {
326                 myContext.log(objectName + ":" + msg);
327             }
328         } /* if we log this level */
329
330     } /* log(int, String, String, String) */
331
332
333     /**
334      * Log a message with the username and job number recorded as well
335      *
336      * @param newLevel Level of this message
337      * @param objectName Calling object
338      * @param msg Message to log
339      * @param color Color to log the message
340      * @param uid User ID
341      * @param jobNumber Job Number
342      */

343     public static void log(int newLevel, String JavaDoc objectName, String JavaDoc msg,
344                            String JavaDoc color, String JavaDoc uid, String JavaDoc jobNumber)
345             throws LogException {
346         String JavaDoc myName = (thisClass + "log(int, String, String, " +
347                 "String, String, String)");
348
349         if (newLevel <= maxLevel) {
350             if (consoleOutput) {
351                 System.err.println(objectName + ":" + msg + ":" + uid +
352                         ":" + jobNumber);
353             }
354
355             DBConnection myConnection = null;
356
357             if (checkLog()) {
358                 try {
359                     myConnection = myPool.getConnection(myName);
360
361                     LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
362                     myLog.setField("ObjectName", objectName);
363                     myLog.setField("MessageText", msg);
364                     myLog.setField("MessageColor", encode(color));
365                     myLog.setField("ExpUid", uid);
366                     myLog.setField("JobNumber", jobNumber);
367                     myLog.setField("MessageLevel", "" + newLevel);
368                     myLog.add();
369                 } catch (DBException de) {
370                     System.err.println(myName + ":Unable to log '" + msg +
371                             "'");
372                     de.printStackTrace(System.err);
373                 } finally {
374                     if (myPool != null) {
375                         myPool.release(myConnection);
376                     }
377                 }
378             }
379             if (myContext != null) {
380                 myContext.log(objectName + ":" + msg + ":" + uid + ":" +
381                         jobNumber);
382             }
383         } /* if we log this level */
384
385     } /* log(int, String, String, String, String, String) */
386
387
388     /**
389      * Log an exception
390      *
391      * @param e Exception to log
392      * @throws LogException If the log entry cannot be written
393      */

394     public static void log(Exception JavaDoc e)
395             throws LogException {
396         String JavaDoc myName = (thisClass + "log(Exception)");
397         String JavaDoc message = e.getMessage();
398
399         if (e instanceof DBException) {
400             DBException de = (DBException) e;
401             message = de.getMessage() + ":" + de.getDBMessage();
402         }
403         if (consoleOutput) {
404             System.err.println(message);
405             e.printStackTrace();
406         }
407
408         DBConnection myConnection = null;
409
410         if (checkLog()) {
411             try {
412                 myConnection = myPool.getConnection(myName);
413
414                 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
415                 ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
416                 e.printStackTrace(new PrintStream JavaDoc(bos));
417                 myLog.setField("MessageText", bos.toString());
418                 myLog.setField("MessageColor", encode("RED"));
419                 myLog.add();
420             } catch (DBException de) {
421                 System.err.println(myName + ":Unable to log exception");
422                 e.printStackTrace(System.err);
423                 de.printStackTrace(System.err);
424             } finally {
425                 if (myPool != null) {
426                     myPool.release(myConnection);
427                 }
428             }
429         }
430         if (myContext != null) {
431             myContext.log("unknown", e);
432         }
433     } /* log(Exception) */
434
435
436     /**
437      * Log the given message at the level 0 (e.g. always log)
438      *
439      * @param msg Message to log
440      */

441     public static void log(String JavaDoc msg)
442             throws LogException {
443         log(0, msg);
444     } /* log(String) */
445
446
447     /**
448      * Log an exception from a particular object
449      *
450      * @param objectName Calling object
451      * @param e Exception to log
452      */

453     public static void log(String JavaDoc objectName, Exception JavaDoc e)
454             throws LogException {
455         String JavaDoc myName = (thisClass + "log(String, Exception)");
456         String JavaDoc message = e.getMessage();
457
458         if (e instanceof DBException) {
459             DBException de = (DBException) e;
460             message = de.getMessage() + ":" + de.getDBMessage();
461         }
462         if (consoleOutput) {
463             System.err.println(objectName + ":" + message);
464             e.printStackTrace(System.err);
465         }
466
467         DBConnection myConnection = null;
468
469         if (checkLog()) {
470             try {
471                 myConnection = myPool.getConnection(myName);
472
473                 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
474                 ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
475                 e.printStackTrace(new PrintStream JavaDoc(bos));
476                 myLog.setField("MessageText", bos.toString());
477                 myLog.setField("MessageColor", encode("RED"));
478                 myLog.setField("ObjectName", objectName);
479                 myLog.add();
480             } catch (DBException de) {
481                 throw new LogException(myName + ":" + de.getMessage());
482             } finally {
483                 if (myPool != null) {
484                     myPool.release(myConnection);
485                 }
486             }
487         }
488         if (myContext != null) {
489             myContext.log(objectName, e);
490         }
491     } /* log(String, Exception) */
492
493
494     /**
495      * Log the given message at level 0 from the named object
496      *
497      * @param objectName Calling object
498      * @param msg Message to log
499      * @throws LogException if the message cannot be logged
500      */

501     public static void log(String JavaDoc objectName, String JavaDoc msg)
502             throws LogException {
503         log(0, objectName, msg);
504     } /* log(String, String) */
505
506
507     /**
508      * Log the given message from an object at level 0
509      *
510      * @param objectName Calling object
511      * @param msg Message to log
512      * @param color Color to log the message with
513      * @throws LogException if the message cannot be logged
514      */

515     public static void log(String JavaDoc objectName, String JavaDoc msg, String JavaDoc color)
516             throws LogException {
517         log(0, objectName, msg, color);
518     } /* log(String, String, String) */
519
520
521     /**
522      * Log a message at level 0
523      *
524      * @param objectName Calling object
525      * @param msg Message to log
526      * @param color Color to log the message
527      * @param userName User Name
528      * @param jobNumber Job Number
529      */

530     public static void log(String JavaDoc objectName, String JavaDoc msg, String JavaDoc color,
531                            String JavaDoc userName, String JavaDoc jobNumber)
532             throws LogException {
533         log(0, objectName, msg, color, userName, jobNumber);
534     } /* log(String, String, String, String, String) */
535
536
537     /**
538      * Turn on or off console output of the log
539      *
540      * @param newFlag True if log messages are echoed to the console as
541      * well as other logs, else false
542      */

543     public static void setConsoleOutput(boolean newFlag) {
544         consoleOutput = newFlag;
545     } /* setConsoleOutput(boolean) */
546
547     /**
548      * Set the servlet context, so we have access to the servlet log
549      *
550      * @param newContext The servlet context to be used to log
551      */

552     public static void setContext(ServletContext JavaDoc newContext)
553             throws LogException {
554         String JavaDoc myName = (thisClass + "setContext(ServletContext)");
555
556         if (newContext == null) {
557             log(myName, "Null context", "RED");
558
559             return;
560         }
561         if (myContext == null) {
562             myContext = newContext;
563         }
564     } /* setContext(ServletContext) */
565
566
567     /**
568      * Set the max size that a log is allowed to reach before it is
569      * auto-archived to the old directory
570      *
571      * @param newMax Max size of the new log in records
572      */

573     public static void setMax(int newMax) {
574         if (newMax < 0) {
575             maxLevel = 0;
576         } else if (newMax > 9) {
577             maxLevel = 9;
578         } else {
579             maxLevel = newMax;
580         }
581     } /* setMax(int) */
582
583     /**
584      * Log a message at a given level with an objectname, message,
585      * and color. Does not throw exception if message cannot be logged
586      *
587      * @param newLevel
588      * @param objectName
589      * @param msg
590      * @param color
591      */

592     public static void tryLog(int newLevel, String JavaDoc objectName, String JavaDoc msg,
593                               String JavaDoc color) {
594         String JavaDoc myName = (thisClass + "tryLog(int, String, String, " +
595                 "String)");
596
597         try {
598             log(newLevel, objectName, msg, color);
599         } catch (LogException he) {
600             System.err.println(myName + ":Unable to log message '" + msg +
601                     "' from object '" + objectName + "':" +
602                     he.getMessage());
603             he.printStackTrace();
604         }
605     } /* tryLog(int, String, String, String) */
606
607     /**
608      * Call to log that does not throw any exception, but writes to standard
609      * error if the message cannot be logged correctly
610      *
611      * @param objectName Name of the object logging the message
612      * @param e Exception being logged
613      */

614     public static void tryLog(String JavaDoc objectName, Exception JavaDoc e) {
615         String JavaDoc myName = (thisClass + "tryLog(String, Exception)");
616         String JavaDoc message = e.getMessage();
617
618         if (e instanceof DBException) {
619             DBException de = (DBException) e;
620             message = de.getMessage() + ":" + de.getDBMessage();
621         }
622         try {
623             log(objectName, e);
624         } catch (LogException he) {
625             System.err.println(myName + ":Unable to log exception '" +
626                     message + "' from object '" + objectName +
627                     "':" + he.getMessage());
628             he.printStackTrace(System.err);
629             e.printStackTrace(System.err);
630         }
631     } /* tryLog(String, Exception) */
632
633     /**
634      * Try to log the given message from the given object at level 0
635      * but don't throw an exception if it doesn't work
636      *
637      * @param objectName
638      * @param msg
639      */

640     public static void tryLog(String JavaDoc objectName, String JavaDoc msg) {
641         String JavaDoc myName = (thisClass + "tryLog(String, String)");
642
643         try {
644             log(objectName, msg);
645         } catch (LogException he) {
646             System.err.println(myName + ":Unable to log message '" + msg +
647                     "' from object '" + objectName + "':" +
648                     he.getMessage());
649             he.printStackTrace();
650         }
651     } /* tryLog(String, String) */
652
653     /**
654      * Try to log a message at level 0 given the objectname, message, and
655      * color. No exception if unable to be logged.
656      *
657      * @param objectName
658      * @param msg
659      * @param color
660      */

661     public static void tryLog(String JavaDoc objectName, String JavaDoc msg, String JavaDoc color) {
662         String JavaDoc myName = (thisClass + "tryLog(String, String, " +
663                 "String)");
664
665         try {
666             log(objectName, msg, color);
667         } catch (LogException he) {
668             System.err.println(myName + ":Unable to log message '" + msg +
669                     "' from object '" + objectName + "':" +
670                     he.getMessage());
671             he.printStackTrace();
672         }
673     } /* tryLog(String, String, String) */
674
675     /**
676      * Try to log a message given the object, message, color
677      * username and job number. No exception if message cannot be logged
678      *
679      * @param objectName
680      * @param msg
681      * @param color
682      * @param userName
683      * @param jobNumber
684      */

685     public static void tryLog(String JavaDoc objectName, String JavaDoc msg, String JavaDoc color,
686                               String JavaDoc userName, String JavaDoc jobNumber) {
687         String JavaDoc myName = (thisClass + "tryLog(String, String, " +
688                 "String, String, String)");
689
690         try {
691             log(objectName, msg, color, userName, jobNumber);
692         } catch (LogException he) {
693             System.err.println(myName + ":Unable to log message '" + msg +
694                     "' from object '" + objectName + "':" +
695                     he.getMessage());
696             he.printStackTrace();
697         }
698     } /* tryLog(String, String, String, String, String) */
699
700 } /* Log */
701
702
Popular Tags