7

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
Mornedhel
  • 163
  • 1
  • 10
  • Let's work on why Logger.log(String...) doesn't work. The Javadoc notes that it is intended to be used with Wrapper classes, so you seem to be on the right track. Can you post more information than "doesn't seem to be working?" – Mark Peters May 25 '10 at 14:40
  • Putting either the source name ("java.lang.Integer" in the above case) or the wrapper Fully Qualified Class Name ("test.Logger2") in the long Logger.log(String...) call doesnt seem to affect the output either way. – Mornedhel May 25 '10 at 15:15
  • that sounds a lot like you need to edit your log4j.properties file and change your logging pattern – Sean Patrick Floyd May 25 '10 at 15:18
  • @seanizer: How so? If I change the "c" to "C" the faux source goes from "test.Logger2" to "?". – Mornedhel May 25 '10 at 16:50
  • sorry, I'm not a master in the arts of log4j config :-( – Sean Patrick Floyd May 25 '10 at 18:13

3 Answers3

6

My home brewed logging solution used log4j's LocationInfo class to find the source code information.

With this solution, the locationInfo object contains information from the object that calls my logger with the loggerName.

Here's a simplified version of my logger that logs with log4j:

public void log(Level level, String message) {
    LocationInfo locationInfo = new LocationInfo(new Throwable(),
            loggerName);

    MDC.put(LINE_NUMBER, locationInfo.getLineNumber());
    MDC.put(FILE_NAME, locationInfo.getFileName());
    MDC.put(CLASS_NAME, locationInfo.getClassName());
    MDC.put(METHOD_NAME, locationInfo.getMethodName());
    MDC.put(FQMETHOD_NAME, locationInfo.getClassName() + "."
            + locationInfo.getMethodName());

    logger.log(level, message);

    MDC.remove(LINE_NUMBER);
    MDC.remove(FILE_NAME);
    MDC.remove(CLASS_NAME);
    MDC.remove(METHOD_NAME);
    MDC.remove(FQMETHOD_NAME);
}

Btw: The Level, MDC and Logger class are all log4j classes.

Replies to comments:

The MDC object is stored on the ThreadLocal object and is accessible for the log4j logger.

From the MDC Java documentation:

The MDC is managed on a per thread basis.

Espen
  • 10,545
  • 5
  • 33
  • 39
0

This should do it:

class Logger2 {

  Logger _log4JLogger;

  public void log(Object msg) {
    _log4JLogger.log(Logger2.class.getName(), Priority.INFO, msg, null);
  }

}
jake256
  • 192
  • 1
  • 3
0

In Log4J API about Logger.log(String callerFQCN, Priority level, Object message, Throwable t):

Parameters:

  • callerFQCN - The wrapper class' fully qualified class name.
  • level - The level of the logging request.
  • message - The message of the logging request.
  • t - The throwable of the logging request, may be null.

See: Log4J API.

Quite weird I know, argument is called "callerFQCN" which seams to be the caller object class, BUT (muajaja!) it's actually the "wrapper" class (ex MyClass.class.getName()).

Actually, the caller is get by the Throwable argument I think. Not sure, haven't checked the implementation.

As you leave Throwable argument null, I think something like this must be happening below:

StackTraceElement[] stack = (new Throwable()).getStackTrace();
String caller = stack[something, meaby 1].getClassName();

If you want to create a Logger wrapper you need to do something like this:

public static void myWarn(String message) {
    myLogger.log(MyWrapper.class.getName(), message, Level.WARN, null);
}

This work without problem as a wrapper.

Or, if you absolutely need to maintain the interface you show below, do something like this:

// Warning: Pseudocode
public static void myWarn(Object source, String message) {
    String sourceClass = source.class.GetName();
    StackTraceElement[] stack = (new Throwable()).getStackTrace();
    stack[1, or meaby 2... or 0?] = new whatever(sourceClass);
    myLogger.log(MyWrapper.class.getName(), message, Level.WARN, myStack);
}

Hope this helps.

Havok
  • 5,776
  • 1
  • 35
  • 44