1 24 25 package com.jdon.util; 26 27 import java.io.PrintStream ; 28 import java.io.PrintWriter ; 29 import java.text.DateFormat ; 30 31 import org.apache.log4j.Category; 32 import org.apache.log4j.Logger; 33 import org.apache.log4j.Priority; 34 35 36 44 public final class Debug { 45 46 public final static String LOG = "log.level"; 47 public final static String LOG4J = "log.log4j"; 48 49 public final static String SETUPNAME = "setup"; 50 public final static String SETUPVALUE = "true"; 51 52 public static boolean useLog4J = false; 53 private static Logger logger = null; 54 static DateFormat 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 [] levels = {"Always", "Verbose", "Timing", "Info", "Important", "Warning", "Error", "Fatal"}; 70 public static final String [] 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 printStream = System.out; 74 protected static PrintWriter printWriter = new PrintWriter (printStream); 75 76 public static PrintStream getPrintStream() { 77 return printStream; 78 } 79 80 public static void setPrintStream(PrintStream printStream) { 81 Debug.printStream = printStream; 82 Debug.printWriter = new PrintWriter (printStream); 83 } 84 85 public static PrintWriter getPrintWriter() { 86 return printWriter; 87 } 88 89 public static Category getLogger(String 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 t, String msg, String 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 levelStrs = propsUtil.getProperty(LOG); 106 if (levelStrs != null) { 107 conf_level = Integer.parseInt(levelStrs); 108 } 109 110 String log4jStrs = propsUtil.getProperty(LOG4J); 111 if (log4jStrs != null) 112 if (log4jStrs.equalsIgnoreCase("true")) 113 useLog4J = true; 114 115 } 116 catch (Exception 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 t, String msg, String module, String callingClass) { 128 129 if (level >= getConfLogLevel()){ 130 if (useLog4J) { 131 Category logger = getLogger(module); 133 logger.log(callingClass, levelObjs[level], msg, t); 134 } else { 135 StringBuffer prefixBuf = new StringBuffer (); 136 prefixBuf.append(dateFormat.format(new java.util.Date ())); 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 msg) { 162 log(Debug.ALWAYS, null, msg, null); 163 } 164 public static void log(String msg, String module) { 165 log(Debug.ALWAYS, null, msg, module); 166 } 167 public static void log(Throwable t) { 168 log(Debug.ALWAYS, t, null, null); 169 } 170 public static void log(Throwable t, String msg) { 171 log(Debug.ALWAYS, t, msg, null); 172 } 173 public static void log(Throwable t, String msg, String 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 msg) { 181 log(Debug.VERBOSE, null, msg, null); 182 } 183 public static void logVerbose(String msg, String module) { 184 log(Debug.VERBOSE, null, msg, module); 185 } 186 public static void logVerbose(Throwable t) { 187 log(Debug.VERBOSE, t, null, null); 188 } 189 public static void logVerbose(Throwable t, String msg) { 190 log(Debug.VERBOSE, t, msg, null); 191 } 192 public static void logVerbose(Throwable t, String msg, String 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 msg) { 200 log(Debug.TIMING, null, msg, null); 201 } 202 public static void logTiming(String msg, String module) { 203 log(Debug.TIMING, null, msg, module); 204 } 205 public static void logTiming(Throwable t) { 206 log(Debug.TIMING, t, null, null); 207 } 208 public static void logTiming(Throwable t, String msg) { 209 log(Debug.TIMING, t, msg, null); 210 } 211 public static void logTiming(Throwable t, String msg, String 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 msg) { 219 log(Debug.INFO, null, msg, null); 220 } 221 public static void logInfo(String msg, String module) { 222 log(Debug.INFO, null, msg, module); 223 } 224 public static void logInfo(Throwable t) { 225 log(Debug.INFO, t, null, null); 226 } 227 public static void logInfo(Throwable t, String msg) { 228 log(Debug.INFO, t, msg, null); 229 } 230 public static void logInfo(Throwable t, String msg, String 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 msg) { 238 log(Debug.IMPORTANT, null, msg, null); 239 } 240 public static void logImportant(String msg, String module) { 241 log(Debug.IMPORTANT, null, msg, module); 242 } 243 public static void logImportant(Throwable t) { 244 log(Debug.IMPORTANT, t, null, null); 245 } 246 public static void logImportant(Throwable t, String msg) { 247 log(Debug.IMPORTANT, t, msg, null); 248 } 249 public static void logImportant(Throwable t, String msg, String 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 msg) { 257 log(Debug.WARNING, null, msg, null); 258 } 259 public static void logWarning(String msg, String module) { 260 log(Debug.WARNING, null, msg, module); 261 } 262 public static void logWarning(Throwable t) { 263 log(Debug.WARNING, t, null, null); 264 } 265 public static void logWarning(Throwable t, String msg) { 266 log(Debug.WARNING, t, msg, null); 267 } 268 public static void logWarning(Throwable t, String msg, String 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 msg) { 276 log(Debug.ERROR, null, msg, null); 277 } 278 public static void logError(String msg, String module) { 279 log(Debug.ERROR, null, msg, module); 280 } 281 public static void logError(Throwable t) { 282 log(Debug.ERROR, t, null, null); 283 } 284 public static void logError(Throwable t, String msg) { 285 log(Debug.ERROR, t, msg, null); 286 } 287 public static void logError(Throwable t, String msg, String 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 msg) { 295 log(Debug.FATAL, null, msg, null); 296 } 297 public static void logFatal(String msg, String module) { 298 log(Debug.FATAL, null, msg, module); 299 } 300 public static void logFatal(Throwable t) { 301 log(Debug.FATAL, t, null, null); 302 } 303 public static void logFatal(Throwable t, String msg) { 304 log(Debug.FATAL, t, msg, null); 305 } 306 public static void logFatal(Throwable t, String msg, String module) { 307 log(Debug.FATAL, t, msg, module); 308 } 309 } 310 | Popular Tags |