Extreme Java Logging
-
A few things to note:
-logger is already an instance of log4j, which does pretty much everything anyway
-this code has been copied entirely from another source code file in the same directory
-I have no idea WTF is going on here.public void warn(Object msg, Throwable t) {
logger.log(FQCN, Level.WARN, msg, t);
logNestedException(Level.WARN, msg, t);
}void logNestedException(Level level, Object msg, Throwable t) { if(t == null) return; try { Class tC = t.getClass(); Method mA[] = tC.getMethods(); Method nextThrowableMethod = null; for(int i=0; i < mA.length ; i++) { if(("getCause".equals(mA[i].getName()) && !JDK14) || "getRootCause".equals(mA[i].getName()) || "getNextException".equals( mA[i].getName()) || "getException".equals( mA[i].getName())) { // check param types Class params[] = mA[i].getParameterTypes(); if(params==null || params.length==0) { // just found the getter for the nested throwable nextThrowableMethod=mA[i]; break; // no need to search further } } } if(nextThrowableMethod != null) { // get the nested throwable and log it Throwable nextT=(Throwable)nextThrowableMethod.invoke(t, new Object[0]); if(nextT != null) { this.logger.log(FQCN, level, "Previous log CONTINUED: ", nextT); } } } catch(Exception e) { // do nothing } }
-
@sinister_prog This code logs the message from a
Throwable
object (usually probably anException
), and then uses Java's built-in class meta info accessors to check whether that object is wrapping anotherThrowable
object and logs that one, too.
-
@djls45 Which log4j normally would do as well through the standard
getCause
method (added in Java 1.4), so it looks like this code actually is built to detect exception chaining from before Java 1.4.If it is that old then I wish the OP all the best of luck...
-
@sinister_prog said in Extreme Java Logging:
!JDK14
Let's use reflection and iterate over all the methods of the throwable regardless of whether this is true or false in the first place!