我想记录 WebView 控制台事件。他们有时会发现所使用的底层浏览器中的怪癖,并可以帮助排除故障。
可以使用 Sun 实现类与 WebView 控制台交互:
import
//...
WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) ->
LOGGER.info(() -> "WebConsoleListener: " + message + "[" + webEngine.getLocation() + ":" + lineNumber + "]")
);
Run Code Online (Sandbox Code Playgroud)
但是,com.sun.javafx.webkit.WebConsoleListener是一个实现类,而不是 JavaFX 公共 API 的一部分。
用于获取 JavaFX WebView 控制台事件的公共 API 是什么?
或者,获取这些事件进行故障排除的正确方法是什么?
您可以通过 Java\xe2\x84\xa2 2 平台的核心日志记录工具启用浏览器控制台日志记录,方法是将其添加到logging.properties:
\n\ncom.sun.webkit.WebPage.level = FINE\n
Run Code Online (Sandbox Code Playgroud)\n\n确保日志记录配置中存在 FINE 或更低级别的日志处理程序,否则日志将在记录之前被过滤。例子:
\n\nhandlers = java.util.logging.ConsoleHandler\n.level = INFO\njava.util.logging.ConsoleHandler.level = ALL\njava.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter\n\ncom.sun.webkit.WebPage.level = FINE\n
Run Code Online (Sandbox Code Playgroud)\n\n这是我如何想到这一点的更深入的解释:
\n\nWebConsoleListener#setDefaultListener(WebConsoleListener)调用WebPageClientImpl#setConsoleListener(WebConsoleListener)。
WebPageClientImpl#setConsoleListener(WebConsoleListener)将侦听器存储在其静态字段consoleListener中。
consoleListener仅与WebPageClientImpl#addMessageToConsole(String,int,String)交互。
WebPageClientImpl#addMessageToConsole(String,int,String)覆盖WebPageClient#addMessageToConsole(String,int,String)。
WebPageClient#addMessageToConsole(String,int,String) is called by WebPage#fwkAddMessageToConsole(String,int,String). There are no other call sites in the code base at the time of this writing.
That same method logs the console information:
\n\ncom.sun.webkit.WebPage.level = FINE\n
Run Code Online (Sandbox Code Playgroud)\n\nThat means you can get the logging you need by enabling FINE logging on com.sun.webkit.WebPage limiting the implementation-level dependency to logging configuration:
\n\ncom.sun.webkit.WebPage.level = FINE\n
Run Code Online (Sandbox Code Playgroud)\n\nI could not find a public API for this.
\n\nBased on my review of the OpenJDK JFX repo source, there isn\'t a public API for this.
\n\nThis solution is still not ideal as it depends on a private implementation classname, but that dependency is in a configuration file where if the implementation class changes or disappears, the impact is a loss of logging rather than a likely fatal NoClassDefFoundError or NoSuchMethodError.
\n 归档时间: |
|
查看次数: |
591 次 |
最近记录: |