打印“来源”带有 log4j 包装器的日志语句中的类

发布于 2024-09-02 17:09:55 字数 1125 浏览 1 评论 0原文

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

除了为每个日志语句创建新的 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);
    }
}

当调用时:

public void testWarning() {
    Logger2.warning(new Integer(3), "This should warn");
}

打印:

Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN                              test.Logger2 - This should warn

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抽个烟儿 2024-09-09 17:09:55

我自制的日志记录解决方案使用了 log4j 的 LocationInfo 类查找源代码信息。

通过此解决方案,locationInfo 对象包含来自使用 loggerName 调用我的记录器的对象的信息。

这是我的记录器的简化版本,使用 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);
}

顺便说一句: 级别MDCLogger 类都是 log4j 类。

评论回复:

MDC 对象存储在 ThreadLocal 对象上,可供 log4j 记录器访问。

来自 MDC Java 文档:

MDC 是按线程进行管理的。

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.

心碎无痕… 2024-09-09 17:09:55

在 Log4J API 中,关于 Logger.log(String callerFQCN, Priority level, Object message, Throwable t):

参数:

  • callerFQCN - 包装类的完全限定类名。
  • level - 日志记录请求的级别。
  • message - 日志记录请求的消息。
  • t - 日志请求的 throwable,可以为 null。

请参阅:Log4J API

我知道很奇怪,参数被称为“callerFQCN”,它似乎是调用者对象类,但(muajaja!)它实际上是“包装器”类(例如 MyClass.class.getName())。

实际上,我认为调用者是通过 Throwable 参数得到的。不确定,尚未检查实施情况。

当您将 Throwable 参数保留为空时,我认为下面一定会发生类似的事情:

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

如果您想创建一个 Logger 包装器,您需要执行如下操作:

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

作为包装器,这项工作没有问题。

或者,如果您绝对需要维护下面显示的界面,请执行以下操作:

// 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);
}

希望这会有所帮助。

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.

梦旅人picnic 2024-09-09 17:09:55

这应该可以做到:

class Logger2 {

  Logger _log4JLogger;

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

}

This should do it:

class Logger2 {

  Logger _log4JLogger;

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文