001: /*
002: * $Id: Debug.java,v 1.4 2003/08/20 21:04:43 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.io.PrintStream;
027: import java.io.PrintWriter;
028: import java.text.DateFormat;
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import org.apache.log4j.Logger;
034: import org.apache.log4j.Level;
035: import org.apache.log4j.PropertyConfigurator;
036: import org.apache.log4j.spi.LoggerRepository;
037:
038: /**
039: * Configurable Debug logging wrapper class
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.4 $
044: * @since 2.0
045: */
046:public final class Debug {
047:
048: public static final boolean useLog4J = true;
049: public static final String noModuleModule = "NoModule"; // set to null for previous behavior
050:
051: static DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
052:
053: public static final String SYS_DEBUG = System.getProperty("DEBUG");
054: public static final int ALWAYS = 0;
055: public static final int VERBOSE = 1;
056: public static final int TIMING = 2;
057: public static final int INFO = 3;
058: public static final int IMPORTANT = 4;
059: public static final int WARNING = 5;
060: public static final int ERROR = 6;
061: public static final int FATAL = 7;
062:
063: public static final String[] levels = {"Always", "Verbose", "Timing", "Info", "Important", "Warning", "Error", "Fatal"};
064: public static final String[] levelProps = {"", "print.verbose", "print.timing", "print.info", "print.important", "print.warning", "print.error", "print.fatal"};
065: public static final Level[] levelObjs = {Level.INFO, Level.DEBUG, Level.DEBUG, Level.INFO, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL};
066:
067: protected static Map levelStringMap = new HashMap();
068:
069: protected static PrintStream printStream = System.out;
070: protected static PrintWriter printWriter = new PrintWriter(printStream);
071:
072: protected static boolean levelOnCache[] = new boolean[8];
073: protected static final boolean useLevelOnCache = true;
074:
075: protected static Logger root = Logger.getRootLogger();
076:
077: static {
078: levelStringMap.put("verbose", new Integer(Debug.VERBOSE));
079: levelStringMap.put("timing", new Integer(Debug.TIMING));
080: levelStringMap.put("info", new Integer(Debug.INFO));
081: levelStringMap.put("important", new Integer(Debug.IMPORTANT));
082: levelStringMap.put("warning", new Integer(Debug.WARNING));
083: levelStringMap.put("error", new Integer(Debug.ERROR));
084: levelStringMap.put("fatal", new Integer(Debug.FATAL));
085: levelStringMap.put("always", new Integer(Debug.ALWAYS));
086:
087: // initialize Log4J
088: PropertyConfigurator.configure(FlexibleProperties.makeFlexibleProperties(UtilURL.fromResource("debug.properties")));
089:
090: // initialize levelOnCache
091: for (int i = 0; i < 8; i++) {
092: levelOnCache[i] = (i == Debug.ALWAYS || UtilProperties.propertyValueEqualsIgnoreCase("debug.properties", levelProps[i], "true"));
093: }
094:
095: if (SYS_DEBUG != null) {
096: for (int x = 0; x < 8; x++) {
097: levelOnCache[x] = true;
098: }
099: LoggerRepository repo = root.getLoggerRepository();
100: Enumeration enum = repo.getCurrentLoggers();
101: while (enum.hasMoreElements()) {
102: Logger this Logger = (Logger) enum.nextElement();
103: this Logger.setLevel(Level.DEBUG);
104: }
105: }
106: }
107:
108: public static PrintStream getPrintStream() {
109: return printStream;
110: }
111:
112: public static void setPrintStream(PrintStream printStream) {
113: Debug.printStream = printStream;
114: Debug.printWriter = new PrintWriter(printStream);
115: }
116:
117: public static PrintWriter getPrintWriter() {
118: return printWriter;
119: }
120:
121: public static Logger getLogger(String module) {
122: if (module != null && module.length() > 0) {
123: return Logger.getLogger(module);
124: } else {
125: return root;
126: }
127: }
128:
129: /** Gets an Integer representing the level number from a String representing the level name; will return null if not found */
130: public static Integer getLevelFromString(String levelName) {
131: if (levelName == null) return null;
132: return (Integer) levelStringMap.get(levelName.toLowerCase());
133: }
134:
135: /** Gets an int representing the level number from a String representing the level name; if level not found defaults to Debug.INFO */
136: public static int getLevelFromStringWithDefault(String levelName) {
137: Integer levelInt = getLevelFromString(levelName);
138: if (levelInt == null) {
139: return Debug.INFO;
140: } else {
141: return levelInt.intValue();
142: }
143: }
144:
145: public static void log(int level, Throwable t, String msg, String module) {
146: log(level, t, msg, module, "org.ofbiz.base.util.Debug");
147: }
148:
149: public static void log(int level, Throwable t, String msg, String module, String callingClass) {
150: if (isOn(level)) {
151: if (useLog4J) {
152: Logger logger = getLogger(module);
153: if (SYS_DEBUG != null) {
154: logger.setLevel(Level.DEBUG);
155: }
156: logger.log(callingClass, levelObjs[level], msg, t);
157: } else {
158: StringBuffer prefixBuf = new StringBuffer();
159:
160: prefixBuf.append(dateFormat.format(new java.util.Date()));
161: prefixBuf.append(" [OFBiz");
162: if (module != null) {
163: prefixBuf.append(":");
164: prefixBuf.append(module);
165: }
166: prefixBuf.append(":");
167: prefixBuf.append(levels[level]);
168: prefixBuf.append("] ");
169: if (msg != null) {
170: getPrintWriter().print(prefixBuf.toString());
171: getPrintWriter().println(msg);
172: }
173: if (t != null) {
174: getPrintWriter().print(prefixBuf.toString());
175: getPrintWriter().println("Received throwable:");
176: t.printStackTrace(getPrintWriter());
177: }
178: }
179: }
180: }
181:
182: public static boolean isOn(int level) {
183: if (useLevelOnCache) {
184: return levelOnCache[level];
185: } else {
186: return (level == Debug.ALWAYS || UtilProperties.propertyValueEqualsIgnoreCase("debug", levelProps[level], "true"));
187: }
188: }
189:
190: // leaving these here
191: public static void log(String msg) {
192: log(Debug.ALWAYS, null, msg, noModuleModule);
193: }
194: public static void log(Throwable t) {
195: log(Debug.ALWAYS, t, null, noModuleModule);
196: }
197:
198: public static void log(String msg, String module) {
199: log(Debug.ALWAYS, null, msg, module);
200: }
201:
202:
203: public static void log(Throwable t, String module) {
204: log(Debug.ALWAYS, t, null, module);
205: }
206:
207: public static void log(Throwable t, String msg, String module) {
208: log(Debug.ALWAYS, t, msg, module);
209: }
210:
211: public static boolean verboseOn() {
212: return isOn(Debug.VERBOSE);
213: }
214:
215: public static void logVerbose(String msg, String module) {
216: log(Debug.VERBOSE, null, msg, module);
217: }
218:
219: public static void logVerbose(Throwable t, String module) {
220: log(Debug.VERBOSE, t, null, module);
221: }
222:
223: public static void logVerbose(Throwable t, String msg, String module) {
224: log(Debug.VERBOSE, t, msg, module);
225: }
226:
227: public static boolean timingOn() {
228: return isOn(Debug.TIMING);
229: }
230:
231: public static void logTiming(String msg, String module) {
232: log(Debug.TIMING, null, msg, module);
233: }
234:
235: public static void logTiming(Throwable t, String module) {
236: log(Debug.TIMING, t, null, module);
237: }
238:
239: public static void logTiming(Throwable t, String msg, String module) {
240: log(Debug.TIMING, t, msg, module);
241: }
242:
243: public static boolean infoOn() {
244: return isOn(Debug.INFO);
245: }
246:
247: public static void logInfo(String msg, String module) {
248: log(Debug.INFO, null, msg, module);
249: }
250:
251: public static void logInfo(Throwable t, String module) {
252: log(Debug.INFO, t, null, module);
253: }
254:
255: public static void logInfo(Throwable t, String msg, String module) {
256: log(Debug.INFO, t, msg, module);
257: }
258:
259: public static boolean importantOn() {
260: return isOn(Debug.IMPORTANT);
261: }
262:
263: public static void logImportant(String msg, String module) {
264: log(Debug.IMPORTANT, null, msg, module);
265: }
266:
267: public static void logImportant(Throwable t, String module) {
268: log(Debug.IMPORTANT, t, null, module);
269: }
270:
271: public static void logImportant(Throwable t, String msg, String module) {
272: log(Debug.IMPORTANT, t, msg, module);
273: }
274:
275: public static boolean warningOn() {
276: return isOn(Debug.WARNING);
277: }
278:
279: public static void logWarning(String msg, String module) {
280: log(Debug.WARNING, null, msg, module);
281: }
282:
283: public static void logWarning(Throwable t, String module) {
284: log(Debug.WARNING, t, null, module);
285: }
286:
287: public static void logWarning(Throwable t, String msg, String module) {
288: log(Debug.WARNING, t, msg, module);
289: }
290:
291: public static boolean errorOn() {
292: return isOn(Debug.ERROR);
293: }
294:
295: public static void logError(String msg, String module) {
296: log(Debug.ERROR, null, msg, module);
297: }
298:
299: public static void logError(Throwable t, String module) {
300: log(Debug.ERROR, t, null, module);
301: }
302:
303: public static void logError(Throwable t, String msg, String module) {
304: log(Debug.ERROR, t, msg, module);
305: }
306:
307: public static boolean fatalOn() {
308: return isOn(Debug.FATAL);
309: }
310:
311: public static void logFatal(String msg, String module) {
312: log(Debug.FATAL, null, msg, module);
313: }
314:
315: public static void logFatal(Throwable t, String module) {
316: log(Debug.FATAL, t, null, module);
317: }
318:
319: public static void logFatal(Throwable t, String msg, String module) {
320: log(Debug.FATAL, t, msg, module);
321: }
322:
323: public static void set(int level, boolean on) {
324: if (!useLevelOnCache)
325: return;
326: levelOnCache[level] = on;
327: }
328:}
|