KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > Debug


1 /*
2  * $Id: Debug.java,v 1.2 2005/01/31 05:27:54 jdon Exp $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package com.jdon.util;
26
27 import java.io.PrintStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.text.DateFormat JavaDoc;
30
31 import org.apache.log4j.Category;
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.Priority;
34
35
36 /**
37  * Configurable Debug logging wrapper class
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @author <a HREF="mailto:jaz@zsolv.com">Andy Zeneski</a>
41  * @version 1.0
42  * @created July 1, 2001
43  */

44 public final class Debug {
45
46     public final static String JavaDoc LOG = "log.level";
47     public final static String JavaDoc LOG4J = "log.log4j";
48
49     public final static String JavaDoc SETUPNAME = "setup";
50     public final static String JavaDoc SETUPVALUE = "true";
51
52     public static boolean useLog4J = false;
53     private static Logger logger = null;
54     static DateFormat JavaDoc dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
55
56     private final static PropsUtil propsUtil = new PropsUtil("log.xml");
57
58     public static final int ALWAYS = 0;
59     public static final int VERBOSE = 1;
60     public static final int TIMING = 2;
61     public static final int INFO = 3;
62     public static final int IMPORTANT = 4;
63     public static final int WARNING = 5;
64     public static final int ERROR = 6;
65     public static final int FATAL = 7;
66
67     public static int conf_level = -1;
68
69     public static final String JavaDoc[] levels = {"Always", "Verbose", "Timing", "Info", "Important", "Warning", "Error", "Fatal"};
70     public static final String JavaDoc[] levelProps = {"", "print.verbose", "print.timing", "print.info", "print.important", "print.warning", "print.error", "print.fatal"};
71     public static final Priority[] levelObjs = {Priority.INFO, Priority.DEBUG, Priority.DEBUG, Priority.INFO, Priority.INFO, Priority.WARN, Priority.ERROR, Priority.FATAL};
72
73     protected static PrintStream JavaDoc printStream = System.out;
74     protected static PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(printStream);
75
76     public static PrintStream JavaDoc getPrintStream() {
77         return printStream;
78     }
79
80     public static void setPrintStream(PrintStream JavaDoc printStream) {
81         Debug.printStream = printStream;
82         Debug.printWriter = new PrintWriter JavaDoc(printStream);
83     }
84
85     public static PrintWriter JavaDoc getPrintWriter() {
86         return printWriter;
87     }
88
89     public static Category getLogger(String JavaDoc module) {
90         if (module != null && module.length() > 0) {
91             return Category.getInstance(module);
92         } else {
93             return Logger.getLogger(Debug.class);
94         }
95     }
96
97     public static void log(int level, Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
98         log(level, t, msg, module, "com.jdon.util.Debug");
99     }
100
101
102     public static int getConfLogLevel() {
103       if (conf_level == -1) {
104         try {
105           String JavaDoc levelStrs = propsUtil.getProperty(LOG);
106           if (levelStrs != null) {
107             conf_level = Integer.parseInt(levelStrs);
108           }
109
110           String JavaDoc log4jStrs = propsUtil.getProperty(LOG4J);
111           if (log4jStrs != null)
112              if (log4jStrs.equalsIgnoreCase("true"))
113                  useLog4J = true;
114
115         }
116         catch (Exception JavaDoc e) {
117           System.err.print("getLogLevel e");
118           conf_level = 1;
119           useLog4J = false;
120         }
121       }
122       return conf_level;
123
124     }
125
126
127     public static void log(int level, Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module, String JavaDoc callingClass) {
128
129         if (level >= getConfLogLevel()){
130             if (useLog4J) {
131 // logger = Logger.getLogger(module);
132
Category logger = getLogger(module);
133                 logger.log(callingClass, levelObjs[level], msg, t);
134             } else {
135                 StringBuffer JavaDoc prefixBuf = new StringBuffer JavaDoc();
136                 prefixBuf.append(dateFormat.format(new java.util.Date JavaDoc()));
137                 prefixBuf.append(" [Debug");
138                 if (module != null) {
139                     prefixBuf.append(":");
140                     prefixBuf.append(module);
141                 }
142                 prefixBuf.append(":");
143                 prefixBuf.append(levels[level]);
144                 prefixBuf.append("] ");
145                 if (msg != null) {
146                     getPrintStream().print(prefixBuf.toString());
147                     getPrintStream().println(msg);
148                 }
149                 if (t != null) {
150                     getPrintStream().print(prefixBuf.toString());
151                     getPrintStream().println("Received throwable:");
152                     t.printStackTrace(getPrintStream());
153                 }
154             }
155         }
156     }
157
158     public static boolean isOn(int level) {
159         return (level == Debug.ALWAYS);
160     }
161     public static void log(String JavaDoc msg) {
162         log(Debug.ALWAYS, null, msg, null);
163     }
164     public static void log(String JavaDoc msg, String JavaDoc module) {
165         log(Debug.ALWAYS, null, msg, module);
166     }
167     public static void log(Throwable JavaDoc t) {
168         log(Debug.ALWAYS, t, null, null);
169     }
170     public static void log(Throwable JavaDoc t, String JavaDoc msg) {
171         log(Debug.ALWAYS, t, msg, null);
172     }
173     public static void log(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
174         log(Debug.ALWAYS, t, msg, module);
175     }
176
177     public static boolean verboseOn() {
178         return isOn(Debug.VERBOSE);
179     }
180     public static void logVerbose(String JavaDoc msg) {
181         log(Debug.VERBOSE, null, msg, null);
182     }
183     public static void logVerbose(String JavaDoc msg, String JavaDoc module) {
184         log(Debug.VERBOSE, null, msg, module);
185     }
186     public static void logVerbose(Throwable JavaDoc t) {
187         log(Debug.VERBOSE, t, null, null);
188     }
189     public static void logVerbose(Throwable JavaDoc t, String JavaDoc msg) {
190         log(Debug.VERBOSE, t, msg, null);
191     }
192     public static void logVerbose(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
193         log(Debug.VERBOSE, t, msg, module);
194     }
195
196     public static boolean timingOn() {
197         return isOn(Debug.TIMING);
198     }
199     public static void logTiming(String JavaDoc msg) {
200         log(Debug.TIMING, null, msg, null);
201     }
202     public static void logTiming(String JavaDoc msg, String JavaDoc module) {
203         log(Debug.TIMING, null, msg, module);
204     }
205     public static void logTiming(Throwable JavaDoc t) {
206         log(Debug.TIMING, t, null, null);
207     }
208     public static void logTiming(Throwable JavaDoc t, String JavaDoc msg) {
209         log(Debug.TIMING, t, msg, null);
210     }
211     public static void logTiming(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
212         log(Debug.TIMING, t, msg, module);
213     }
214
215     public static boolean infoOn() {
216         return isOn(Debug.INFO);
217     }
218     public static void logInfo(String JavaDoc msg) {
219         log(Debug.INFO, null, msg, null);
220     }
221     public static void logInfo(String JavaDoc msg, String JavaDoc module) {
222         log(Debug.INFO, null, msg, module);
223     }
224     public static void logInfo(Throwable JavaDoc t) {
225         log(Debug.INFO, t, null, null);
226     }
227     public static void logInfo(Throwable JavaDoc t, String JavaDoc msg) {
228         log(Debug.INFO, t, msg, null);
229     }
230     public static void logInfo(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
231         log(Debug.INFO, t, msg, module);
232     }
233
234     public static boolean importantOn() {
235         return isOn(Debug.IMPORTANT);
236     }
237     public static void logImportant(String JavaDoc msg) {
238         log(Debug.IMPORTANT, null, msg, null);
239     }
240     public static void logImportant(String JavaDoc msg, String JavaDoc module) {
241         log(Debug.IMPORTANT, null, msg, module);
242     }
243     public static void logImportant(Throwable JavaDoc t) {
244         log(Debug.IMPORTANT, t, null, null);
245     }
246     public static void logImportant(Throwable JavaDoc t, String JavaDoc msg) {
247         log(Debug.IMPORTANT, t, msg, null);
248     }
249     public static void logImportant(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
250         log(Debug.IMPORTANT, t, msg, module);
251     }
252
253     public static boolean warningOn() {
254         return isOn(Debug.WARNING);
255     }
256     public static void logWarning(String JavaDoc msg) {
257         log(Debug.WARNING, null, msg, null);
258     }
259     public static void logWarning(String JavaDoc msg, String JavaDoc module) {
260         log(Debug.WARNING, null, msg, module);
261     }
262     public static void logWarning(Throwable JavaDoc t) {
263         log(Debug.WARNING, t, null, null);
264     }
265     public static void logWarning(Throwable JavaDoc t, String JavaDoc msg) {
266         log(Debug.WARNING, t, msg, null);
267     }
268     public static void logWarning(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
269         log(Debug.WARNING, t, msg, module);
270     }
271
272     public static boolean errorOn() {
273         return isOn(Debug.ERROR);
274     }
275     public static void logError(String JavaDoc msg) {
276         log(Debug.ERROR, null, msg, null);
277     }
278     public static void logError(String JavaDoc msg, String JavaDoc module) {
279         log(Debug.ERROR, null, msg, module);
280     }
281     public static void logError(Throwable JavaDoc t) {
282         log(Debug.ERROR, t, null, null);
283     }
284     public static void logError(Throwable JavaDoc t, String JavaDoc msg) {
285         log(Debug.ERROR, t, msg, null);
286     }
287     public static void logError(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
288         log(Debug.ERROR, t, msg, module);
289     }
290
291     public static boolean fatalOn() {
292         return isOn(Debug.FATAL);
293     }
294     public static void logFatal(String JavaDoc msg) {
295         log(Debug.FATAL, null, msg, null);
296     }
297     public static void logFatal(String JavaDoc msg, String JavaDoc module) {
298         log(Debug.FATAL, null, msg, module);
299     }
300     public static void logFatal(Throwable JavaDoc t) {
301         log(Debug.FATAL, t, null, null);
302     }
303     public static void logFatal(Throwable JavaDoc t, String JavaDoc msg) {
304         log(Debug.FATAL, t, msg, null);
305     }
306     public static void logFatal(Throwable JavaDoc t, String JavaDoc msg, String JavaDoc module) {
307         log(Debug.FATAL, t, msg, module);
308     }
309 }
310
Popular Tags