1 16 17 18 package org.apache.commons.logging.impl; 19 20 21 import java.io.Serializable ; 22 import java.util.logging.Level ; 23 import java.util.logging.Logger ; 24 import java.util.logging.LogRecord ; 25 import java.util.StringTokenizer ; 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 29 import org.apache.commons.logging.Log; 30 31 32 44 45 public class Jdk13LumberjackLogger implements Log, Serializable { 46 47 48 50 51 54 protected transient Logger logger = null; 55 protected String name = null; 56 private String sourceClassName = "unknown"; 57 private String sourceMethodName = "unknown"; 58 private boolean classAndMethodFound = false; 59 60 61 67 protected static final Level dummyLevel = Level.FINE; 68 69 71 72 77 public Jdk13LumberjackLogger(String name) { 78 79 this.name = name; 80 logger = getLogger(); 81 82 } 83 84 85 87 88 private void log( Level level, String msg, Throwable ex ) { 89 if( getLogger().isLoggable(level) ) { 90 LogRecord record = new LogRecord (level, msg); 91 if( !classAndMethodFound ) { 92 getClassAndMethod(); 93 } 94 record.setSourceClassName(sourceClassName); 95 record.setSourceMethodName(sourceMethodName); 96 if( ex != null ) { 97 record.setThrown(ex); 98 } 99 getLogger().log(record); 100 } 101 } 102 103 107 private void getClassAndMethod() { 108 try { 109 Throwable throwable = new Throwable (); 110 throwable.fillInStackTrace(); 111 StringWriter stringWriter = new StringWriter (); 112 PrintWriter printWriter = new PrintWriter ( stringWriter ); 113 throwable.printStackTrace( printWriter ); 114 String traceString = stringWriter.getBuffer().toString(); 115 StringTokenizer tokenizer = 116 new StringTokenizer ( traceString, "\n" ); 117 tokenizer.nextToken(); 118 String line = tokenizer.nextToken(); 119 while ( line.indexOf( this.getClass().getName() ) == -1 ) { 120 line = tokenizer.nextToken(); 121 } 122 while ( line.indexOf( this.getClass().getName() ) >= 0 ) { 123 line = tokenizer.nextToken(); 124 } 125 int start = line.indexOf( "at " ) + 3; 126 int end = line.indexOf( '(' ); 127 String temp = line.substring( start, end ); 128 int lastPeriod = temp.lastIndexOf( '.' ); 129 sourceClassName = temp.substring( 0, lastPeriod ); 130 sourceMethodName = temp.substring( lastPeriod + 1 ); 131 } catch ( Exception ex ) { 132 } 134 classAndMethodFound = true; 135 } 136 137 143 public void debug(Object message) { 144 log(Level.FINE, String.valueOf(message), null); 145 } 146 147 148 155 public void debug(Object message, Throwable exception) { 156 log(Level.FINE, String.valueOf(message), exception); 157 } 158 159 160 166 public void error(Object message) { 167 log(Level.SEVERE, String.valueOf(message), null); 168 } 169 170 171 178 public void error(Object message, Throwable exception) { 179 log(Level.SEVERE, String.valueOf(message), exception); 180 } 181 182 183 189 public void fatal(Object message) { 190 log(Level.SEVERE, String.valueOf(message), null); 191 } 192 193 194 201 public void fatal(Object message, Throwable exception) { 202 log(Level.SEVERE, String.valueOf(message), exception); 203 } 204 205 206 209 public Logger getLogger() { 210 if (logger == null) { 211 logger = Logger.getLogger(name); 212 } 213 return (logger); 214 } 215 216 217 223 public void info(Object message) { 224 log(Level.INFO, String.valueOf(message), null); 225 } 226 227 228 235 public void info(Object message, Throwable exception) { 236 log(Level.INFO, String.valueOf(message), exception); 237 } 238 239 240 243 public boolean isDebugEnabled() { 244 return (getLogger().isLoggable(Level.FINE)); 245 } 246 247 248 251 public boolean isErrorEnabled() { 252 return (getLogger().isLoggable(Level.SEVERE)); 253 } 254 255 256 259 public boolean isFatalEnabled() { 260 return (getLogger().isLoggable(Level.SEVERE)); 261 } 262 263 264 267 public boolean isInfoEnabled() { 268 return (getLogger().isLoggable(Level.INFO)); 269 } 270 271 272 275 public boolean isTraceEnabled() { 276 return (getLogger().isLoggable(Level.FINEST)); 277 } 278 279 280 283 public boolean isWarnEnabled() { 284 return (getLogger().isLoggable(Level.WARNING)); 285 } 286 287 288 294 public void trace(Object message) { 295 log(Level.FINEST, String.valueOf(message), null); 296 } 297 298 299 306 public void trace(Object message, Throwable exception) { 307 log(Level.FINEST, String.valueOf(message), exception); 308 } 309 310 311 317 public void warn(Object message) { 318 log(Level.WARNING, String.valueOf(message), null); 319 } 320 321 322 329 public void warn(Object message, Throwable exception) { 330 log(Level.WARNING, String.valueOf(message), exception); 331 } 332 333 334 } 335 | Popular Tags |