小编Mor*_*hel的帖子

使用log4j包装器在日志语句中打印“ source”类

我的应用程序有一个自制的日志记录类,我将在后台使用log4j进行迁移。但是,由于我正在使用homebrew类将应用程序的其余日志记录语句传递给log4j,因此输出语句被记录为来自包装类而不是源类。

除了为每个log语句创建新的org.apache.log4j.Logger实例之外,是否有办法确保显示“正确的”源?我也尝试过使用Logger.log(String callerFQCN,Priority level,Object message,Throwable t)方法,但是它似乎不起作用,例如:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

当调用者:

public void testWarning() {
    Logger2.warning(new Integer(3), "This should warn");
}
Run Code Online (Sandbox Code Playgroud)

印刷品:

Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN                              test.Logger2 - …
Run Code Online (Sandbox Code Playgroud)

java log4j

7
推荐指数
1
解决办法
5382
查看次数

标签 统计

java ×1

log4j ×1