Reputation: 21716
I want to log WebView console events. They sometimes pick up quirks in the underlying browser used and can help with troubleshooting.
It is possible to use an Sun implementation class to interact with the WebView console:
import
//...
WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) ->
LOGGER.info(() -> "WebConsoleListener: " + message + "[" + webEngine.getLocation() + ":" + lineNumber + "]")
);
However, com.sun.javafx.webkit.WebConsoleListener, is an implementation class and not part of the JavaFX public API.
What is the public API for getting JavaFX WebView console events?
Alternatively, what is the correct way to get these events for troubleshooting?
Upvotes: 5
Views: 733
Reputation: 21716
You can enable browser console logging via Java™ 2 platform's core logging facilities by adding this to logging.properties:
com.sun.webkit.WebPage.level = FINE
Make sure that a log handler with FINE or lower level is present in the logging configuration or the logs will be filtered before they are logged. Example:
handlers = java.util.logging.ConsoleHandler
.level = INFO
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter
com.sun.webkit.WebPage.level = FINE
Here's a more in-depth explanation of how I figured that out:
WebConsoleListener#setDefaultListener(WebConsoleListener) calls WebPageClientImpl#setConsoleListener(WebConsoleListener).
WebPageClientImpl#setConsoleListener(WebConsoleListener) stores the listener in its static field consoleListener.
consoleListener is only interacted with by WebPageClientImpl#addMessageToConsole(String,int,String).
WebPageClientImpl#addMessageToConsole(String,int,String) overrides 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:
log.log(Level.FINE, "fwkAddMessageToConsole(): message = " + message
+ ", lineNumber = " + lineNumber + ", sourceId = " + sourceId);
That 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:
com.sun.webkit.WebPage.level = FINE
I could not find a public API for this.
Based on my review of the OpenJDK JFX repo source, there isn't a public API for this.
This 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.
Upvotes: 7