1 22 23 24 package com.mchange.v2.log.jdk14logging; 25 26 import java.util.*; 27 import java.util.logging.*; 28 import com.mchange.v2.log.*; 29 import com.mchange.v2.util.DoubleWeakHashMap; 30 31 public final class Jdk14MLog extends MLog 32 { 33 private static String [] UNKNOWN_ARRAY = new String [] {"UNKNOWN_CLASS", "UNKNOWN_METHOD"}; 34 35 private final static String CHECK_CLASS = "java.util.logging.Logger"; 36 37 private final Map namedLoggerMap = new DoubleWeakHashMap(); 38 39 MLogger global = null; 40 41 public Jdk14MLog() throws ClassNotFoundException 42 { Class.forName( CHECK_CLASS ); } 43 44 public synchronized MLogger getMLogger(String name) 45 { 46 MLogger out = (MLogger) namedLoggerMap.get( name ); 47 if (out == null) 48 { 49 Logger lg = Logger.getLogger(name); 50 out = new Jdk14MLogger( lg ); 51 namedLoggerMap.put( name, out ); 52 } 53 return out; 54 } 55 56 public synchronized MLogger getMLogger(Class cl) 57 { return getLogger( cl.getName() ); } 58 59 60 public synchronized MLogger getMLogger() 61 { 62 if (global == null) 63 global = new Jdk14MLogger( LogManager.getLogManager().getLogger("global") ); 64 return global; 65 } 66 67 72 private static String [] findCallingClassAndMethod() 73 { 74 StackTraceElement [] ste = new Throwable ().getStackTrace(); 75 for (int i = 0, len = ste.length; i < len; ++i) 76 { 77 StackTraceElement check = ste[i]; 78 String cn = check.getClassName(); 79 if (cn != null && ! cn.startsWith("com.mchange.v2.log.jdk14logging")) 80 return new String [] { check.getClassName(), check.getMethodName() }; 81 } 82 return UNKNOWN_ARRAY; 83 } 84 85 86 87 private final static class Jdk14MLogger implements MLogger 88 { 89 Logger logger; 90 91 Jdk14MLogger( Logger logger ) 92 { 93 this.logger = logger; 94 } 96 97 private static Level level(MLevel lvl) 98 { return (Level) lvl.asJdk14Level(); } 99 100 public ResourceBundle getResourceBundle() 101 { return logger.getResourceBundle(); } 102 103 public String getResourceBundleName() 104 { return logger.getResourceBundleName(); } 105 106 public void setFilter(Object java14Filter) throws SecurityException 107 { 108 if (! (java14Filter instanceof Filter)) 109 throw new IllegalArgumentException ("MLogger.setFilter( ... ) requires a java.util.logging.Filter. " + 110 "This is not enforced by the compiler only to permit building under jdk 1.3"); 111 logger.setFilter( (Filter) java14Filter ); 112 } 113 114 public Object getFilter() 115 { return logger.getFilter(); } 116 117 public void log(MLevel l, String msg) 118 { 119 if (! logger.isLoggable( level(l) )) return; 120 121 String [] sa = findCallingClassAndMethod(); 122 logger.logp( level(l), sa[0], sa[1], msg ); 123 } 124 125 public void log(MLevel l, String msg, Object param) 126 { 127 if (! logger.isLoggable( level(l) )) return; 128 129 String [] sa = findCallingClassAndMethod(); 130 logger.logp( level(l), sa[0], sa[1], msg, param ); 131 } 132 133 public void log(MLevel l,String msg, Object [] params) 134 { 135 if (! logger.isLoggable( level(l) )) return; 136 137 String [] sa = findCallingClassAndMethod(); 138 logger.logp( level(l), sa[0], sa[1], msg, params ); 139 } 140 141 public void log(MLevel l, String msg, Throwable t) 142 { 143 if (! logger.isLoggable( level(l) )) return; 144 145 String [] sa = findCallingClassAndMethod(); 146 logger.logp( level(l), sa[0], sa[1], msg, t ); 147 } 148 149 public void logp(MLevel l, String srcClass, String srcMeth, String msg) 150 { 151 if (! logger.isLoggable( level(l) )) return; 152 153 if (srcClass == null && srcMeth == null) 154 { 155 String [] sa = findCallingClassAndMethod(); 156 srcClass = sa[0]; 157 srcMeth = sa[1]; 158 } 159 logger.logp( level(l), srcClass, srcMeth, msg ); 160 } 161 162 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object param) 163 { 164 if (! logger.isLoggable( level(l) )) return; 165 166 if (srcClass == null && srcMeth == null) 167 { 168 String [] sa = findCallingClassAndMethod(); 169 srcClass = sa[0]; 170 srcMeth = sa[1]; 171 } 172 logger.logp( level(l), srcClass, srcMeth, msg, param ); 173 } 174 175 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object [] params) 176 { 177 if (! logger.isLoggable( level(l) )) return; 178 179 if (srcClass == null && srcMeth == null) 180 { 181 String [] sa = findCallingClassAndMethod(); 182 srcClass = sa[0]; 183 srcMeth = sa[1]; 184 } 185 logger.logp( level(l), srcClass, srcMeth, msg, params ); 186 } 187 188 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Throwable t) 189 { 190 if (! logger.isLoggable( level(l) )) return; 191 192 if (srcClass == null && srcMeth == null) 193 { 194 String [] sa = findCallingClassAndMethod(); 195 srcClass = sa[0]; 196 srcMeth = sa[1]; 197 } 198 logger.logp( level(l), srcClass, srcMeth, msg, t ); 199 } 200 201 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg) 202 { 203 if (! logger.isLoggable( level(l) )) return; 204 205 if (srcClass == null && srcMeth == null) 206 { 207 String [] sa = findCallingClassAndMethod(); 208 srcClass = sa[0]; 209 srcMeth = sa[1]; 210 } 211 logger.logrb( level(l), srcClass, srcMeth, rb, msg ); 212 } 213 214 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object param) 215 { 216 if (! logger.isLoggable( level(l) )) return; 217 218 if (srcClass == null && srcMeth == null) 219 { 220 String [] sa = findCallingClassAndMethod(); 221 srcClass = sa[0]; 222 srcMeth = sa[1]; 223 } 224 logger.logrb( level(l), srcClass, srcMeth, rb, msg, param ); 225 } 226 227 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object [] params) 228 { 229 if (! logger.isLoggable( level(l) )) return; 230 231 if (srcClass == null && srcMeth == null) 232 { 233 String [] sa = findCallingClassAndMethod(); 234 srcClass = sa[0]; 235 srcMeth = sa[1]; 236 } 237 logger.logrb( level(l), srcClass, srcMeth, rb, msg, params ); 238 } 239 240 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Throwable t) 241 { 242 if (! logger.isLoggable( level(l) )) return; 243 244 if (srcClass == null && srcMeth == null) 245 { 246 String [] sa = findCallingClassAndMethod(); 247 srcClass = sa[0]; 248 srcMeth = sa[1]; 249 } 250 logger.logrb( level(l), srcClass, srcMeth, rb, msg, t ); 251 } 252 253 public void entering(String srcClass, String srcMeth) 254 { 255 if (! logger.isLoggable( Level.FINER )) return; 256 257 logger.entering( srcClass, srcMeth ); 258 } 259 260 public void entering(String srcClass, String srcMeth, Object param) 261 { 262 if (! logger.isLoggable( Level.FINER )) return; 263 264 logger.entering( srcClass, srcMeth, param ); 265 } 266 267 public void entering(String srcClass, String srcMeth, Object params[]) 268 { 269 if (! logger.isLoggable( Level.FINER )) return; 270 271 logger.entering( srcClass, srcMeth, params ); 272 } 273 274 public void exiting(String srcClass, String srcMeth) 275 { 276 if (! logger.isLoggable( Level.FINER )) return; 277 278 logger.exiting( srcClass, srcMeth ); 279 } 280 281 public void exiting(String srcClass, String srcMeth, Object result) 282 { 283 if (! logger.isLoggable( Level.FINER )) return; 284 285 logger.exiting( srcClass, srcMeth, result ); 286 } 287 288 public void throwing(String srcClass, String srcMeth, Throwable t) 289 { 290 if (! logger.isLoggable( Level.FINER )) return; 291 292 logger.throwing( srcClass, srcMeth, t ); 293 } 294 295 public void severe(String msg) 296 { 297 if (! logger.isLoggable( Level.SEVERE )) return; 298 299 String [] sa = findCallingClassAndMethod(); 300 logger.logp( Level.SEVERE, sa[0], sa[1], msg ); 301 } 302 303 public void warning(String msg) 304 { 305 if (! logger.isLoggable( Level.WARNING )) return; 306 307 String [] sa = findCallingClassAndMethod(); 308 logger.logp( Level.WARNING, sa[0], sa[1], msg ); 309 } 310 311 public void info(String msg) 312 { 313 if (! logger.isLoggable( Level.INFO )) return; 314 315 String [] sa = findCallingClassAndMethod(); 316 logger.logp( Level.INFO, sa[0], sa[1], msg ); 317 } 318 319 public void config(String msg) 320 { 321 if (! logger.isLoggable( Level.CONFIG )) return; 322 323 String [] sa = findCallingClassAndMethod(); 324 logger.logp( Level.CONFIG, sa[0], sa[1], msg ); 325 } 326 327 public void fine(String msg) 328 { 329 if (! logger.isLoggable( Level.FINE )) return; 330 331 String [] sa = findCallingClassAndMethod(); 332 logger.logp( Level.FINE, sa[0], sa[1], msg ); 333 } 334 335 public void finer(String msg) 336 { 337 if (! logger.isLoggable( Level.FINER )) return; 338 339 String [] sa = findCallingClassAndMethod(); 340 logger.logp( Level.FINER, sa[0], sa[1], msg ); 341 } 342 343 public void finest(String msg) 344 { 345 if (! logger.isLoggable( Level.FINEST )) return; 346 347 String [] sa = findCallingClassAndMethod(); 348 logger.logp( Level.FINEST, sa[0], sa[1], msg ); 349 } 350 351 public void setLevel(MLevel l) throws SecurityException 352 { logger.setLevel( level(l) ); } 353 354 public MLevel getLevel() 355 { return MLevel.fromIntValue( logger.getLevel().intValue() ); } 356 357 public boolean isLoggable(MLevel l) 358 { return logger.isLoggable( level(l) ); } 359 360 public String getName() 361 { return logger.getName(); } 362 363 public void addHandler(Object h) throws SecurityException 364 { 365 if (! (h instanceof Handler)) 366 throw new IllegalArgumentException ("MLogger.addHandler( ... ) requires a java.util.logging.Handler. " + 367 "This is not enforced by the compiler only to permit building under jdk 1.3"); 368 logger.addHandler( (Handler) h ); 369 } 370 371 public void removeHandler(Object h) throws SecurityException 372 { 373 if (! (h instanceof Handler)) 374 throw new IllegalArgumentException ("MLogger.removeHandler( ... ) requires a java.util.logging.Handler. " + 375 "This is not enforced by the compiler only to permit building under jdk 1.3"); 376 logger.removeHandler( (Handler) h ); 377 } 378 379 public Object [] getHandlers() 380 { return logger.getHandlers(); } 381 382 public void setUseParentHandlers(boolean uph) 383 { logger.setUseParentHandlers( uph ); } 384 385 public boolean getUseParentHandlers() 386 { return logger.getUseParentHandlers(); } 387 } 388 } | Popular Tags |