List icon Contents

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 set up 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);

var jxBrowserLogger =
        LogManager.getLogManager().getLogger("com.teamdev.jxbrowser");
        
for (var handler : jxBrowserLogger.getHandlers()) {
    jxBrowserLogger.removeHandler(handler);
}        

jxBrowserLogger.addHandler(new SLF4JBridgeHandler());

var 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. Get the JxBrowser logger and remove the existing handlers attached to it.

    var jxBrowserLogger = 
            LogManager.getLogManager().getLogger("com.teamdev.jxbrowser"); 
    
    for (var handler : jxBrowserLogger.getHandlers()) {
        jxBrowserLogger.removeHandler(handler);
    }
    
  3. We use SLF4JBridgeHandler. It routes logs from JUL to SLF4J. Add the SLF4JBridgeHandler to the JxBrowser logger:

    jxBrowserLogger.addHandler(new SLF4JBridgeHandler());
    
  4. To make JxBrowser write log records, create an Engine:

    var 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.
        var jxBrowserLogger =
                LogManager.getLogManager().getLogger("com.teamdev.jxbrowser");

        // Remove existing handlers attached to the JxBrowser logger.
        for (var handler : jxBrowserLogger.getHandlers()) {
            jxBrowserLogger.removeHandler(handler);
        }

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

        // Creating and running Chromium engine.
        var 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.