Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the "source" class in a log statement with a log4j wrapper

Tags:

java

log4j

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
like image 930
Mornedhel Avatar asked May 25 '10 14:05

Mornedhel


People also ask

How do I view log4j logs?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.

How does log4j logging work?

Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called an appender. Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously.

Which log4j component is responsible for logging messages?

Appenders. Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc.


1 Answers

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.

like image 135
Espen Avatar answered Oct 05 '22 02:10

Espen