KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > Log


1 package org.sapia.magnet;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.util.TreeSet JavaDoc;
6
7 import org.apache.log4j.Appender;
8 import org.apache.log4j.ConsoleAppender;
9 import org.apache.log4j.Layout;
10 import org.apache.log4j.Level;
11 import org.apache.log4j.Logger;
12 import org.apache.log4j.PatternLayout;
13 import org.sapia.magnet.domain.LaunchHandlerIF;
14 import org.sapia.util.CompositeException;
15 import org.sapia.util.CompositeRuntimeException;
16 import org.xml.sax.SAXException JavaDoc;
17
18
19 /**
20  * This class act as the central hub for logging application messages. All the messages
21  * the an end user should see on the console should pass by this <code>Log</code> class
22  * instead of using the traditionnal <code>System.out</code> and <code>System.err</code>.
23  *
24  * @author Jean-Cedric Desrochers
25  * <dl>
26  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>.
27  * All Rights Reserved.</dd></dt>
28  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
29  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class Log {
33
34   /** Defines the logger instance for this class. */
35   private static Logger _theLogger;
36   
37   static {
38     Layout aLayout = new PatternLayout("%m%n");
39     Appender anAppender = new ConsoleAppender(aLayout, ConsoleAppender.SYSTEM_OUT);
40
41     _theLogger = Logger.getLogger("magnet");
42     _theLogger.setAdditivity(false);
43     _theLogger.setLevel(Level.INFO);
44     _theLogger.addAppender(anAppender);
45   }
46   
47   /**
48    * Logs the message passed in as an information message.
49    *
50    * @param aMessage The information message to log.
51    */

52   public static void info(String JavaDoc aMessage) {
53     _theLogger.info(aMessage);
54   }
55
56   /**
57    * Logs the message passed in as an information message.
58    *
59    * @param aMessage The information message to log.
60    * @param aLauncher The launcher handler that log the message.
61    */

62   public static void info(String JavaDoc aMessage, LaunchHandlerIF aLauncher) {
63     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
64     aBuffer.append("[").append(aLauncher.getName()).
65             append("::").append(aLauncher.getType()).
66             append("] ").append(aMessage);
67     _theLogger.info(aBuffer.toString());
68   }
69
70   /**
71    * Logs the message passed in as a warning message.
72    *
73    * @param aMessage The warning message to log.
74    */

75   public static void warn(String JavaDoc aMessage) {
76     _theLogger.info("WARNING: " + aMessage);
77   }
78
79   /**
80    * Logs the message passed in as a warning message.
81    *
82    * @param aMessage The warning message to log.
83    * @param aLauncher The launcher handler that log the message.
84    */

85   public static void warn(String JavaDoc aMessage, LaunchHandlerIF aLauncher) {
86     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
87     aBuffer.append("[").append(aLauncher.getName()).
88             append("::").append(aLauncher.getType()).
89             append("] WARNING: ").append(aMessage);
90     _theLogger.info(aBuffer.toString());
91   }
92
93   /**
94    * Logs the message passed in as an error message.
95    *
96    * @param aMessage The error message to log.
97    */

98   public static void error(String JavaDoc aMessage) {
99     _theLogger.info("ERROR: " + aMessage);
100   }
101   
102   /**
103    * Logs the message passed in as an error message.
104    *
105    * @param aMessage The error message to log.
106    * @param aLauncher The launcher handler that log the message.
107    */

108   public static void error(String JavaDoc aMessage, LaunchHandlerIF aLauncher) {
109     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
110     aBuffer.append("[").append(aLauncher.getName()).
111             append("::").append(aLauncher.getType()).
112             append("] ERROR: ").append(aMessage);
113     _theLogger.info(aBuffer.toString());
114   }
115   
116   /**
117    * Logs the message passed in as an error message.
118    *
119    * @param aMessage The error message to log.
120    * @param aCause The exception that caused the error.
121    */

122   public static void error(String JavaDoc aMessage, Throwable JavaDoc aCause) {
123     _theLogger.info("ERROR: " + aMessage, aCause);
124   }
125
126   /**
127    * Extracts the messages from the exception passed in.
128    *
129    * @param anException The exception to format.
130    * @return The formatted string.
131    */

132   public static String JavaDoc extactMessage(Throwable JavaDoc anException) {
133     StringBuffer JavaDoc aMessage = new StringBuffer JavaDoc();
134     
135     int level = 0;
136     while (anException != null) {
137       level++;
138       // Format the message
139
if (level == 1) {
140         aMessage.append(anException.getMessage());
141       } else {
142         aMessage.append("\n\t---> ").append(anException.getMessage());
143       }
144
145       if (anException instanceof CompositeException) {
146         anException = ((CompositeException) anException).getSourceError();
147       } else if (anException instanceof CompositeRuntimeException) {
148         anException = ((CompositeRuntimeException) anException).getSourceError();
149       } else if (anException instanceof SAXException JavaDoc) {
150         anException = ((SAXException JavaDoc) anException).getException();
151       } else {
152         anException = null;
153       }
154     }
155     
156     return aMessage.toString();
157   }
158   
159   /**
160    * Utility method that formats the properties passed into a String format.
161    *
162    * @param someProperties The properties to format.
163    * @return The String format of the properties.
164    */

165   public static String JavaDoc formatProperties(Properties JavaDoc someProperties) {
166     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("\n ---------- listing properties ----------\n");
167                     
168     for (Iterator JavaDoc it = new TreeSet JavaDoc(someProperties.keySet()).iterator(); it.hasNext(); ) {
169       String JavaDoc aName = (String JavaDoc) it.next();
170       String JavaDoc aValue = someProperties.getProperty(aName);
171       aBuffer.append(aName).append("=").append(aValue).append("\n");
172     }
173     
174     return aBuffer.toString();
175   }
176 }
177
Popular Tags