gpt4 book ai didi

java - 类和成员名称的通用 Java Logger 输出 null.null

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:07 24 4
gpt4 key购买 nike

我正在使用带有以下初始化行的通用 java.util.logging.Logger:

private static final Logger _log = Logger.getLogger(Login.class.getName());

然后我像这样使用它(只是一个例子):

    _log.log(Level.FINE, "Session id: {0}.", session.getId());

在日志中有时我可以看到以下内容:

24-Nov-2014 17:26:13.692 FINE [http-nio-80-exec-1] null.null session ID:18BD6989930169F77565EA2D001A5759。

大多数时候,它向我显示调用类和函数正确。这也发生在其他类(class)和成员中。我不明白为什么会这样?这是 Loggger 中的错误吗?

最佳答案

输出看起来像是来自“org.apache.juli.OneLineFormatter”,“null.null”是源类和源方法名称。来自LogRecord.getSourceMethodName文档:

May be null if no information could be obtained.

可能无法确定或已经forced to null.

查看“org.apache.juli.AsyncFileHandler”的源代码有一个错误,该代码未捕获原始调用站点。

您可以创建和 install a filter在 AsynchFileHandler 上强制在线程切换之前计算方法和类名。以下是此类过滤器的示例:

    public class InferCallerFilter implements Filter {

public boolean isLoggable(LogRecord record) {
record.getSourceMethodName(); //Infer caller.
return true;
}
}

即使是 MemoryHandler包含在 JDK 中 gets this wrong .根据 LogRecord 文档:

Therefore, if a logging Handler wants to pass off a LogRecord to another thread, or to transmit it over RMI, and if it wishes to subsequently obtain method name or class name information it should call one of getSourceClassName or getSourceMethodName to force the values to be filled in.

关于java - 类和成员名称的通用 Java Logger 输出 null.null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com