gpt4 book ai didi

java - JDK : how to enable PlatformLogger programmatically

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:57:06 44 4
gpt4 key购买 nike

我需要以编程方式为某些 JDK7 内部类启用日志记录。

这是我在应用程序初始化时所做的:

httpLogger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
httpLogger.setLevel(Level.FINEST);

其中 httpLogger 是一个强引用(为了避免记录器被垃圾收集)。我还将 ConsoleHandler 的级别设置为 ALL。但是我无法获得任何输出。

如果我通过日志记录配置文件执行此操作,它会按预期工作。

我可能是错的,但我认为这与我不了解 Java 7 中引入的 PlatformLogger 有关,而据我所知,它现在用于所有 JDK 内部日志记录。或者也许我只是不明白 J.U.L.好吧。

我想如果我这样做的话它会起作用:

httpLogger = PlatformLogger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
httpLogger.setLevel(Level.FINEST);

但是 PlatformLogger 类在我无法引用的包中。

什么是PlatformLogger

这是 JavaDoc:

Platform logger provides an API for the JRE components to log
messages. This enables the runtime components to eliminate the
static dependency of the logging facility and also defers the
java.util.logging initialization until it is enabled.
In addition, the PlatformLogger API can be used if the logging
module does not exist.

If the logging facility is not enabled, the platform loggers
will output log messages per the default logging configuration
(see below). In this implementation, it does not log the
the stack frame information issuing the log message.

When the logging facility is enabled (at startup or runtime),
the java.util.logging.Logger will be created for each platform
logger and all log messages will be forwarded to the Logger
to handle.

Logging facility is "enabled" when one of the following
conditions is met:
1) a system property "java.util.logging.config.class" or
"java.util.logging.config.file" is set
2) java.util.logging.LogManager or java.util.logging.Logger
is referenced that will trigger the logging initialization.

Default logging configuration:
global logging level = INFO
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Limitation:
<JAVA_HOME>/lib/logging.properties is the system-wide logging
configuration defined in the specification and read in the
default case to configure any java.util.logging.Logger instances.
Platform loggers will not detect if <JAVA_HOME>/lib/logging.properties
is modified. In other words, unless the java.util.logging API
is used at runtime or the logging system properties is set,
the platform loggers will use the default setting described above.
The platform loggers are designed for JDK developers use and
this limitation can be workaround with setting
-Djava.util.logging.config.file system property.

@since 1.7

最佳答案

我的解决方案出现在一个小的自定义小部件演示程序的开头:

// Log all focus messages.
final Logger rootLogger = Logger.getLogger("");
rootLogger.setLevel(Level.ALL);
final ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
// Because there are a lot of focus messages, make them
// a little easier to read by logging only the message.
consoleHandler.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return "FOCUS: " + record.getMessage() + '\n';
}
});
final Logger logger = Logger.getLogger("java.awt.focus.Component");
logger.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
logger.addHandler(consoleHandler);

我不清楚为什么我需要获取然后将 rootLogger 级别设置为 ALL,但这正是我所需要的。

关于java - JDK : how to enable PlatformLogger programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815048/

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