Redirecting JxBrowser Logs With SLF4J

This tutorial shows how to redirect JxBrowser logs with SLF4J.

Prerequisites

To go through this tutorial you will need:

  • Git.
  • Java 8 or higher.
  • A valid JxBrowser license. It can be either Evaluation or Commercial. For more information on licensing please see the Licensing guide.

Why Redirect Logs

JxBrowser logging is based on java.util.logging, a logging framework that comes with JDK.

By default, JxBrowser logs aren’t captured by other, more advanced, logging libraries and may be lost.

In this tutorial, we’re going to demonstrate how to redirect and capture JxBrowser logs with the help of SLF4J.

Setting Up a Project

The code of the example application for this tutorial is available along with other examples from a GitHub repository as a Gradle-based project.

Getting the Code

To get the code please execute the following commands:

$ git clone https://github.com/TeamDev-IP/JxBrowser-Examples
$ cd JxBrowser-Examples/tutorials/jul-logs-redirect

Adding the License

To run this tutorial you need to setup a license key.

Adding Gradle Dependencies

In this example, we use Log4j library with SLF4J facade.

In the build.gradle.kts, we add the following dependencies:

dependencies {
    // Adds a dependency to the SLF4J API.
    implementation("org.slf4j:slf4j-api:1.7.36")
    
    // Adds a dependency to the Log4j binding for SLF4J.
    implementation("org.slf4j:slf4j-log4j12:1.7.36")

    // Adds a dependency with SLF4J handler for JUL.
    implementation("org.slf4j:jul-to-slf4j:1.7.36")
}

Configuring Log4J

The next step is to add log4j.properties file to configure Log4j. Let’s configure logging levels and the path to the log file:

# Define the root logger with the appender file.
log4j.rootLogger = ALL, FILE

# Define the file appender.
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=log/log.out

# Define the layout for file appender.
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Application Code

Add the following code to the main() method:

Logger.level(Level.DEBUG);

java.util.logging.Logger jxBrowserLogger =
        LogManager.getLogManager().getLogger("com.teamdev.jxbrowser");

jxBrowserLogger.addHandler(new SLF4JBridgeHandler());

Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
engine.close();

Now let’s see what we’ve done here:

  1. Set JxBrowser logging level to DEBUG. For more information about JxBrowser logging please see the Logging guide.

    Logger.level(Level.DEBUG);
    
  2. We use SLF4JBridgeHandler. It routes logs from JUL to SLF4J. Add the SLF4JBridgeHandler to the JxBrowser logger:

     java.util.logging.Logger jxBrowserLogger =
         LogManager.getLogManager().getLogger("com.teamdev.jxbrowser");
       
     jxBrowserLogger.addHandler(new SLF4JBridgeHandler());
    
  3. To make JxBrowser write log records, create an Engine:

     Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
     engine.close();
    

Here is the complete Java code:

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.logging.Level;
import com.teamdev.jxbrowser.logging.Logger;
import java.util.logging.LogManager;
import org.slf4j.bridge.SLF4JBridgeHandler;

/**
 * This example demonstrates how to capture JxBrowser logs using SLF4J API.
 */
public final class LoggingRedirectExample {

    public static void main(String[] args) {

        // Configure JxBrowser logging level.
        Logger.level(Level.DEBUG);

        // Get the JxBrowser logger.
        java.util.logging.Logger jxBrowserLogger =
                LogManager.getLogManager().getLogger("com.teamdev.jxbrowser");

        // Add SLF4JBridgeHandler to the JxBrowser logger to route log records to the SLF4j.
        jxBrowserLogger.addHandler(new SLF4JBridgeHandler());

        // Creating and running Chromium engine.
        Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
        engine.close();
    }
}

All JxBrowser logs will be saved in the log/log.out file that was configured in our log4j.properties file.

Summary

In this tutorial, we have demonstrated how you can redirect JxBrowser logs with SLF4J API by configuring Log4j.

Go Top