5

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?

1 Answer 1

7

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:

  1. WebConsoleListener#setDefaultListener(WebConsoleListener) calls WebPageClientImpl#setConsoleListener(WebConsoleListener).

  2. WebPageClientImpl#setConsoleListener(WebConsoleListener) stores the listener in its static field consoleListener.

  3. consoleListener is only interacted with by WebPageClientImpl#addMessageToConsole(String,int,String).

  4. WebPageClientImpl#addMessageToConsole(String,int,String) overrides WebPageClient#addMessageToConsole(String,int,String).

  5. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

which version of OpenJDK?
@JasonS this has been in all versions of OpenJDK since 8u5-b01 in early 2014. github.com/javafxports/openjdk-jfx/blob/… Why do you ask?
I tried it and was unable to get com.sun.webkit.WebPage log events to be emitted for some reason. But I'm using it in a plugin for a derived version of NetBeans, so who knows what is going on. I gave up.
Yeah, this case above was in the middle of an app already configured for logging. You might need to look at how NetBeans or its plugins expose logging. I'm guessing they'd use JUL directly for logging, but based on your experience it seems like it might be customized in some manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.