Printing the "source" class in a log statement with a log4j wrapper
Posted
by Dur4ndal
on Stack Overflow
See other posts from Stack Overflow
or by Dur4ndal
Published on 2010-05-25T14:32:39Z
Indexed on
2010/05/25
15:21 UTC
Read the original article
Hit count: 185
My application has a homebrew logging class that I'm migrating to using log4j under the covers. However, since I'm using the homebrew class to pass the rest of the application's logging statements to log4j, the output statements are logged as coming from the wrapper class instead of the source class.
Is there a way to ensure that the "correct" source is being shown besides creating new org.apache.log4j.Logger instances for every log statement? I've also tried using the Logger.log(String callerFQCN, Priority level, Object message, Throwable t) method, but it doesnt seem to be working, for example:
public class Logger2 {
public static org.apache.log4j.Logger log4JLogger = org.apache.log4j.Logger.getLogger(Logger2.class);
public static void warning(Object source, String message) {
log(source, message, Level.WARN, null)
}
private static void log(Object source, String message, Level level, Throwable t) {
String className = source.getClass().getName();
System.out.println("Logging class should be " + className);
log4JLogger.log(className, loggingLevel, message, t);
}
}
When called by:
public void testWarning() {
Logger2.warning(new Integer(3), "This should warn");
}
Prints:
Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN test.Logger2 - This should warn
© Stack Overflow or respective owner