1 61 62 package org.logicalcobwebs.logging.impl; 63 64 import java.lang.reflect.Method ; 65 import java.lang.reflect.InvocationTargetException ; 66 67 import org.logicalcobwebs.logging.Log; 68 69 79 80 public final class Jdk14Logger implements Log { 81 82 84 85 90 public Jdk14Logger (String name) { 91 try { 92 final Class loggerClass = Class.forName("java.util.logging.Logger"); 93 final Class levelClass = Class.forName("java.util.logging.Level"); 94 final Method getLoggerMethod = loggerClass.getMethod("getLogger", new Class [] {String .class}); 95 logger = getLoggerMethod.invoke(null, new Object []{name}); 96 logpMethod = logger.getClass().getMethod("logp", new Class [] {levelClass, 97 String .class, String .class, String .class}); 98 logpExMethod = logger.getClass().getMethod("logp", new Class [] {levelClass, 99 String .class, String .class, String .class, Throwable .class}); 100 isLoggableMethod = loggerClass.getMethod("isLoggable", new Class [] {levelClass}); 101 getStackTraceMethod = Throwable .class.getMethod("getStackTrace", null); 102 final Class stackTraceClass = Class.forName("java.lang.StackTraceElement"); 103 getClassNameMethod = stackTraceClass.getMethod("getClassName", null); 104 getMethodNameMethod = stackTraceClass.getMethod("getMethodName", null); 105 levelFINEST = levelClass.getField("FINEST").get(null); 106 levelFINE = levelClass.getField("FINE").get(null); 107 levelINFO = levelClass.getField("INFO").get(null); 108 levelWARNING = levelClass.getField("WARNING").get(null); 109 levelSEVERE = levelClass.getField("SEVERE").get(null); 110 } catch (Throwable e) { 111 e.printStackTrace(); 112 throw new RuntimeException ("Could not create Jdk14Logger: " + e.getMessage()); 113 } 114 } 115 116 117 119 120 123 private Object logger = null; 124 125 private Method logpMethod = null; 126 private Method logpExMethod = null; 127 private Method isLoggableMethod = null; 128 private Method getStackTraceMethod = null; 129 private Method getClassNameMethod = null; 130 private Method getMethodNameMethod = null; 131 132 private Object levelFINEST = null; 133 private Object levelFINE = null; 134 private Object levelINFO = null; 135 private Object levelWARNING = null; 136 private Object levelSEVERE = null; 137 138 139 140 141 142 143 144 146 private void log (Object level, String msg, Throwable ex) { 147 Throwable dummyException = new Throwable (); 149 String cname = "unknown"; 150 String method = "unknown"; 151 try { 153 Object locations[] = (Object []) getStackTraceMethod.invoke(dummyException, null); 154 if (locations != null && locations.length > 2) { 156 cname = (String ) getClassNameMethod.invoke(locations[2], null); 157 method = (String ) getMethodNameMethod.invoke(locations[2], null); 158 } 159 } catch (IllegalAccessException e) { 160 e.printStackTrace(); 161 } catch (IllegalArgumentException e) { 162 e.printStackTrace(); 163 } catch (InvocationTargetException e) { 164 e.printStackTrace(); 165 } 166 167 try { 168 if (ex == null) { 169 logpMethod.invoke(logger, new Object []{level, cname, method, msg}); 170 } else { 171 logpExMethod.invoke(logger, new Object []{level, cname, method, msg, ex}); 172 } 173 } catch (Exception e) { 174 throw new RuntimeException ( 175 "Logging of message '" + msg + "' failed" + (ex != null ? ":" + ex.getMessage() : ".")); 176 } 177 } 178 179 182 public void debug (Object message) { 183 log (levelFINE, String.valueOf (message), null); 184 } 185 186 189 public void debug (Object message, Throwable exception) { 190 log (levelFINE, String.valueOf (message), exception); 191 } 192 193 196 public void error (Object message) { 197 log (levelSEVERE, String.valueOf (message), null); 198 } 199 200 203 public void error (Object message, Throwable exception) { 204 log (levelSEVERE, String.valueOf (message), exception); 205 } 206 207 210 public void fatal (Object message) { 211 log (levelSEVERE, String.valueOf (message), null); 212 } 213 214 217 public void fatal (Object message, Throwable exception) { 218 log (levelSEVERE, String.valueOf (message), exception); 219 } 220 221 224 public void info (Object message) { 225 log (levelINFO, String.valueOf (message), null); 226 } 227 228 231 public void info (Object message, Throwable exception) { 232 log (levelINFO, String.valueOf (message), exception); 233 } 234 235 238 public boolean isDebugEnabled () { 239 return (isLoggable (levelFINE)); 240 } 241 242 245 public boolean isErrorEnabled () { 246 return (isLoggable (levelSEVERE)); 247 } 248 249 252 public boolean isFatalEnabled () { 253 return (isLoggable (levelSEVERE)); 254 } 255 256 259 public boolean isInfoEnabled () { 260 return (isLoggable (levelINFO)); 261 } 262 263 266 public boolean isTraceEnabled () { 267 return (isLoggable (levelFINEST)); 268 } 269 270 273 public boolean isWarnEnabled () { 274 return (isLoggable (levelWARNING)); 275 } 276 277 280 public void trace (Object message) { 281 log (levelFINEST, String.valueOf (message), null); 282 } 283 284 287 public void trace (Object message, Throwable exception) { 288 log (levelFINEST, String.valueOf (message), exception); 289 } 290 291 294 public void warn (Object message) { 295 log (levelWARNING, String.valueOf (message), null); 296 } 297 298 301 public void warn (Object message, Throwable exception) { 302 log (levelWARNING, String.valueOf (message), exception); 303 } 304 305 private boolean isLoggable(Object level) { 306 try { 307 return ((Boolean ) isLoggableMethod.invoke(logger, new Object [] {level})).booleanValue(); 308 } catch (Exception e) { 309 throw new RuntimeException ("isLoggable call failed: " + e.getMessage()); 310 } 311 } 312 } 313 314 | Popular Tags |