1 22 23 24 package com.mchange.v2.log; 25 26 import java.text.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import com.mchange.lang.ThrowableUtils; 30 31 public final class FallbackMLog extends MLog 32 { 33 final static MLevel DEFAULT_CUTOFF_LEVEL; 34 35 static 36 { 37 MLevel dflt = null; 38 String dfltName = MLog.CONFIG.getProperty( "com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL" ); 39 if (dfltName != null) 40 dflt = MLevel.fromSeverity( dfltName ); 41 if (dflt == null) 42 dflt = MLevel.INFO; 43 DEFAULT_CUTOFF_LEVEL = dflt; 44 } 45 46 MLogger logger = new FallbackMLogger(); 47 48 public synchronized MLogger getMLogger(String name) 49 { return logger; } 50 51 public MLogger getMLogger(Class cl) 52 { return getLogger( cl.getName() ); } 53 54 55 public MLogger getMLogger() 56 { return logger; } 57 58 private final static class FallbackMLogger implements MLogger 59 { 60 MLevel cutoffLevel = DEFAULT_CUTOFF_LEVEL; 61 62 private void formatrb(MLevel l, String srcClass, String srcMeth, String rbname, String msg, Object [] params, Throwable t) 63 { 64 ResourceBundle rb = ResourceBundle.getBundle( rbname ); 65 if (msg != null && rb != null) 66 { 67 String check = rb.getString( msg ); 68 if (check != null) 69 msg = check; 70 } 71 format( l, srcClass, srcMeth, msg, params, t); 72 } 73 74 private void format(MLevel l, String srcClass, String srcMeth, String msg, Object [] params, Throwable t) 75 { System.err.println( formatString( l, srcClass, srcMeth, msg, params, t ) ); } 76 77 private String formatString(MLevel l, String srcClass, String srcMeth, String msg, Object [] params, Throwable t) 78 { 79 boolean add_parens = (srcMeth != null && ! srcMeth.endsWith(")")); 80 81 StringBuffer sb = new StringBuffer (256); 82 sb.append(l.getLineHeader()); 83 sb.append(' '); 84 if (srcClass != null && srcMeth != null) 85 { 86 sb.append('['); 87 sb.append( srcClass ); 88 sb.append( '.' ); 89 sb.append( srcMeth ); 90 if (add_parens) 91 sb.append("()"); 92 sb.append( ']' ); 93 } 94 else if (srcClass != null) 95 { 96 sb.append('['); 97 sb.append( srcClass ); 98 sb.append( ']' ); 99 } 100 else if (srcMeth != null) 101 { 102 sb.append('['); 103 sb.append( srcMeth ); 104 if (add_parens) 105 sb.append("()"); 106 sb.append( ']' ); 107 } 108 if (msg == null) 109 { 110 if (params != null) 111 { 112 sb.append("params: "); 113 for (int i = 0, len = params.length; i < len; ++i) 114 { 115 if (i != 0) sb.append(", "); 116 sb.append( params[i] ); 117 } 118 } 119 } 120 else 121 { 122 if (params == null) 123 sb.append( msg ); 124 else 125 { 126 MessageFormat mfmt = new MessageFormat( msg ); 127 sb.append( mfmt.format( params ) ); 128 } 129 } 130 131 if (t != null) 132 sb.append( ThrowableUtils.extractStackTrace( t ) ); 133 134 return sb.toString(); 135 } 136 137 public ResourceBundle getResourceBundle() 138 { 139 return null; 141 } 142 143 public String getResourceBundleName() 144 { return null; } 145 146 public void setFilter(Object java14Filter) throws SecurityException 147 { 148 warning("Using FallbackMLog -- Filters not supported!"); 149 } 150 151 public Object getFilter() 152 { 153 return null; 154 } 155 156 public void log(MLevel l, String msg) 157 { 158 if ( isLoggable( l ) ) 159 format( l, null, null, msg, null, null ); 160 } 161 162 public void log(MLevel l, String msg, Object param) 163 { 164 if ( isLoggable( l ) ) 165 format( l, null, null, msg, new Object [] { param }, null ); 166 } 167 168 public void log(MLevel l,String msg, Object [] params) 169 { 170 if ( isLoggable( l ) ) 171 format( l, null, null, msg, params, null ); 172 } 173 174 public void log(MLevel l, String msg, Throwable t) 175 { 176 if ( isLoggable( l ) ) 177 format( l, null, null, msg, null, t ); 178 } 179 180 public void logp(MLevel l, String srcClass, String srcMeth, String msg) 181 { 182 if ( isLoggable( l ) ) 183 format( l, srcClass, srcMeth, msg, null, null ); 184 } 185 186 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object param) 187 { 188 if ( isLoggable( l ) ) 189 format( l, srcClass, srcMeth, msg, new Object [] { param }, null ); 190 } 191 192 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object [] params) 193 { 194 if ( isLoggable( l ) ) 195 format( l, srcClass, srcMeth, msg, params, null ); 196 } 197 198 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Throwable t) 199 { 200 if ( isLoggable( l ) ) 201 format( l, srcClass, srcMeth, msg, null, t ); 202 } 203 204 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg) 205 { 206 if ( isLoggable( l ) ) 207 formatrb( l, srcClass, srcMeth, rb, msg, null, null ); 208 } 209 210 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object param) 211 { 212 if ( isLoggable( l ) ) 213 formatrb( l, srcClass, srcMeth, rb, msg, new Object [] { param }, null ); 214 } 215 216 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object [] params) 217 { 218 if ( isLoggable( l ) ) 219 formatrb( l, srcClass, srcMeth, rb, msg, params, null ); 220 } 221 222 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Throwable t) 223 { 224 if ( isLoggable( l ) ) 225 formatrb( l, srcClass, srcMeth, rb, msg, null, t ); 226 } 227 228 public void entering(String srcClass, String srcMeth) 229 { 230 if ( isLoggable( MLevel.FINER ) ) 231 format(MLevel.FINER, srcClass, srcMeth, "Entering method.", null, null); 232 } 233 234 public void entering(String srcClass, String srcMeth, Object param) 235 { 236 if ( isLoggable( MLevel.FINER ) ) 237 format(MLevel.FINER, srcClass, srcMeth, "Entering method with argument " + param, null, null); 238 } 239 240 public void entering(String srcClass, String srcMeth, Object [] params) 241 { 242 if ( isLoggable( MLevel.FINER ) ) 243 { 244 if (params == null) 245 entering( srcClass, srcMeth ); 246 else 247 { 248 StringBuffer sb = new StringBuffer (128); 249 sb.append("( "); 250 for (int i = 0, len = params.length; i < len; ++i) 251 { 252 if (i != 0) sb.append(", "); 253 sb.append( params[i] ); 254 } 255 sb.append(" )"); 256 format(MLevel.FINER, srcClass, srcMeth, "Entering method with arguments " + sb.toString(), null, null); 257 } 258 } 259 } 260 261 public void exiting(String srcClass, String srcMeth) 262 { 263 if ( isLoggable( MLevel.FINER ) ) 264 format(MLevel.FINER, srcClass, srcMeth, "Exiting method.", null, null); 265 } 266 267 public void exiting(String srcClass, String srcMeth, Object result) 268 { 269 if ( isLoggable( MLevel.FINER ) ) 270 format(MLevel.FINER, srcClass, srcMeth, "Exiting method with result " + result, null, null); 271 } 272 273 public void throwing(String srcClass, String srcMeth, Throwable t) 274 { 275 if ( isLoggable( MLevel.FINE ) ) 276 format(MLevel.FINE, srcClass, srcMeth, "Throwing exception." , null, t); 277 } 278 279 public void severe(String msg) 280 { 281 if ( isLoggable( MLevel.SEVERE ) ) 282 format(MLevel.SEVERE, null, null, msg, null, null); 283 } 284 285 public void warning(String msg) 286 { 287 if ( isLoggable( MLevel.WARNING ) ) 288 format(MLevel.WARNING, null, null, msg, null, null); 289 } 290 291 public void info(String msg) 292 { 293 if ( isLoggable( MLevel.INFO ) ) 294 format(MLevel.INFO, null, null, msg, null, null); 295 } 296 297 public void config(String msg) 298 { 299 if ( isLoggable( MLevel.CONFIG ) ) 300 format(MLevel.CONFIG, null, null, msg, null, null); 301 } 302 303 public void fine(String msg) 304 { 305 if ( isLoggable( MLevel.FINE ) ) 306 format(MLevel.FINE, null, null, msg, null, null); 307 } 308 309 public void finer(String msg) 310 { 311 if ( isLoggable( MLevel.FINER ) ) 312 format(MLevel.FINER, null, null, msg, null, null); 313 } 314 315 public void finest(String msg) 316 { 317 if ( isLoggable( MLevel.FINEST ) ) 318 format(MLevel.FINEST, null, null, msg, null, null); 319 } 320 321 public void setLevel(MLevel l) throws SecurityException 322 { this.cutoffLevel = l; } 323 324 public synchronized MLevel getLevel() 325 { return cutoffLevel; } 326 327 public synchronized boolean isLoggable(MLevel l) 328 { return (l.intValue() >= cutoffLevel.intValue()); } 329 330 public String getName() 331 { return "global"; } 332 333 public void addHandler(Object h) throws SecurityException 334 { 335 warning("Using FallbackMLog -- Handlers not supported."); 336 } 337 338 public void removeHandler(Object h) throws SecurityException 339 { 340 warning("Using FallbackMLog -- Handlers not supported."); 341 } 342 343 public Object [] getHandlers() 344 { 345 warning("Using FallbackMLog -- Handlers not supported."); 346 return new Object [0]; 347 } 348 349 public void setUseParentHandlers(boolean uph) 350 { 351 warning("Using FallbackMLog -- Handlers not supported."); 352 } 353 354 public boolean getUseParentHandlers() 355 { return false; } 356 } 357 } | Popular Tags |