1 9 package javolution.util; 10 11 import javolution.Javolution; 12 import javolution.context.LogContext; 13 import j2me.lang.CharSequence; 14 import j2me.util.logging.Level; 15 import j2me.util.logging.Logger; 16 import j2me.util.logging.LogRecord; 17 18 38 public class StandardLog extends LogContext { 39 40 43 private Logger _logger; 44 45 49 public StandardLog() { 50 this(Logger.getLogger("")); 51 } 52 53 59 public StandardLog(Logger logger) { 60 _logger = logger; 61 } 62 63 68 public final Logger getLogger() { 69 return _logger; 70 } 71 72 80 public static boolean isLoggable(Level level) { 81 LogContext log = (LogContext) LogContext.current(); 82 if (log instanceof StandardLog) { 83 return ((StandardLog) log)._logger.isLoggable(level); 84 } else if (level.intValue() >= Level.WARNING.intValue()) { 85 return log.isWarningLogged(); 86 } else if (level.intValue() >= Level.INFO.intValue()) { 87 return log.isInfoLogged(); 88 } else { 89 return false; 90 } 91 } 92 93 101 public static void log(LogRecord record) { 102 LogContext log = (LogContext) LogContext.current(); 103 if (log instanceof StandardLog) { 104 ((StandardLog) log)._logger.log(record); 105 } else { 106 Throwable error = record.getThrown(); 107 if (error != null) { 108 log.logError(error, toCsq(record.getMessage())); 109 } else if (record.getLevel().intValue() > Level.WARNING.intValue()) { 110 log.logWarning(toCsq(record.getMessage())); 111 } else if (record.getLevel().intValue() > Level.INFO.intValue()) { 112 log.logInfo(toCsq(record.getMessage())); 113 } 114 } 115 } 116 117 124 public static void severe(String msg) { 125 LogContext log = (LogContext) LogContext.current(); 126 if (log instanceof StandardLog) { 127 ((StandardLog) log)._logger.severe(msg); 128 } else { 129 log.logWarning(toCsq(msg)); 130 } 131 } 132 133 139 public static void warning(String msg) { 140 LogContext log = (LogContext) LogContext.current(); 141 log.logWarning(toCsq(msg)); 142 } 143 144 150 public static void info(String msg) { 151 LogContext log = (LogContext) LogContext.current(); 152 log.logInfo(toCsq(msg)); 153 } 154 155 161 public static void config(String msg) { 162 LogContext log = (LogContext) LogContext.current(); 163 if (log instanceof StandardLog) { 164 ((StandardLog) log)._logger.config(msg); 165 } 166 } 167 168 174 public static void fine(String msg) { 175 LogContext log = (LogContext) LogContext.current(); 176 if (log instanceof StandardLog) { 177 ((StandardLog) log)._logger.fine(msg); 178 } 179 } 180 181 187 public static void finer(String msg) { 188 LogContext log = (LogContext) LogContext.current(); 189 if (log instanceof StandardLog) { 190 ((StandardLog) log)._logger.finer(msg); 191 } 192 } 193 194 200 public static void finest(String msg) { 201 LogContext log = (LogContext) LogContext.current(); 202 if (log instanceof StandardLog) { 203 ((StandardLog) log)._logger.finest(msg); 204 } 205 } 206 207 215 public static void throwing(String sourceClass, String sourceMethod, 216 Throwable thrown) { 217 LogContext log = (LogContext) LogContext.current(); 218 if (log instanceof StandardLog) { 219 ((StandardLog) log)._logger.throwing(sourceClass, sourceMethod, 220 thrown); 221 } else { 222 log.logError(thrown, null); 223 } 224 } 225 226 233 public static void entering(String sourceClass, String sourceMethod) { 234 LogContext log = (LogContext) LogContext.current(); 235 if (log instanceof StandardLog) { 236 ((StandardLog) log)._logger.entering(sourceClass, sourceMethod); 237 } 238 } 239 240 247 public static void exiting(String sourceClass, String sourceMethod) { 248 LogContext log = (LogContext) LogContext.current(); 249 if (log instanceof StandardLog) { 250 ((StandardLog) log)._logger.exiting(sourceClass, sourceMethod); 251 } 252 } 253 254 public boolean isInfoLogged() { 256 return _logger.isLoggable(Level.INFO); 257 } 258 259 public boolean isWarningLogged() { 261 return _logger.isLoggable(Level.WARNING); 262 } 263 264 public boolean isErrorLogged() { 266 return _logger.isLoggable(Level.SEVERE); 267 } 268 269 public void logInfo(CharSequence message) { 271 _logger.info(message.toString()); 272 } 273 274 public void logWarning(CharSequence message) { 276 _logger.warning(message.toString()); 277 } 278 279 public void logError(Throwable error, CharSequence message) { 281 String msg = (message == null) ? error.getMessage() : message 282 .toString(); 283 msg = (msg == null) ? error.getClass().getName() : error.getClass() 284 .getName() 285 + " - " + msg; 286 _logger.severe(msg); 287 } 288 289 private static CharSequence toCsq(Object str) { 290 return Javolution.j2meToCharSeq(str); 291 } 292 } | Popular Tags |