【问题标题】:JDK: how to enable PlatformLogger programmaticallyJDK:如何以编程方式启用 PlatformLogger
【发布时间】:2013-12-28 13:22:13
【问题描述】:

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

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

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

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

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

我可能错了,但我认为这与我不理解 Java 7 中引入的 PlatformLogger 以及 - afaik - 现在用于所有 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

【问题讨论】:

  • 这听起来像是一个乱序的日志初始化——你有没有得到默认的 LogManager,它首先读取系统配置?
  • @Petesh。您的意思是在 ConsoleHandler 上设置级别?实际上,我现在正在尝试使用 logging.properties。然后我从该文件中删除为类设置级别的行,然后尝试以编程方式执行此操作.. 即我使用 both 代码和配置文件的情况。仍然无法让它工作。
  • 刚刚在同一个应用程序中进行了测试,我以编程方式为自己的类设置了日志级别。这按预期工作。我这样做的方式与 JDK 内部类相同。这再次指出——我相信——我缺乏理解PlatformLogger
  • @nolan6000 你有没有找到这个问题的答案?另外,为了记录,在您以编程方式尝试之前,您的日志记录配置文件内容是什么?我拼命地试图让java.awt.focus 类之一打印它的调试输出;全部设置为使用 PlatformLogger,但我无法启用它。
  • 不,从未发现如何以编程方式进行操作。我的配置文件只有sun.net.www.protocol.http.HttpURLConnection.level = FINEST,效果很好。

标签: java logging


【解决方案1】:

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

// 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,但这正是我所需要的。

【讨论】:

    【解决方案2】:

    你必须先阅读它在说什么。

     the PlatformLogger API can be used if the logging module does not exist
    

    好吧,如果您想使用该 API,那么您可以使用以下 import 语句。

    import sun.util.logging.PlatformLogger;
    

    以上是对您目的地的完整参考。

    更多参考请访问BUG Tracking of Java

    【讨论】:

    • 嗯。我不会说我想像这样使用 API,我只想为那个特定的 JDK 类启用日志记录,因为它在跟踪发生的事情方面提供了我想要的东西。使用来自“太阳”的东西。 package 并不完全直接,至少对于 Maven 来说不是,默认情况下它不会在编译类路径中包含大多数 JDK 内部类。还没想出来那个但我仍然不想直接引用 JDK 内部类。
    • Afaiu PlatformLogger 背后的理念是,即使 J.U.L.框架不可用。 (拼图项目的努力)。但就我而言,J.U.L. 可用的,因为 JDK 仍然不是模块化的。 PlatformLogger 为 Java 9 中的内容做准备。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2014-03-05
    • 2018-05-09
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多