JBoss Community Archive (Read Only)


Logging with slf4j

Errai now supports using the slf4j logging api on the server and client. This gives you the flexibility of choosing your own logging back-end for your server-side code, while still allowing a uniform logging interface that can be used in shared packages.

What is slf4j?

sl4j is logging abstraction. Using the slf4j api, you can add log statements to your code using a fixed api while maintaining the ability to switch the logging implementation at run-time. For example, the slf4j api can be used with java.util.logging (JUL) as the back-end.

Client-Side Setup

The client-side slf4j code uses the GWT Logging as the back-end. Using slf4j in client-side code has two steps:

  1. Add the errai-common artifact as a maven dependency to your project

  2. Inherit the gwt module org.jboss.errai.common.ErraiCommon

Errai Client-Side Log Handlers

In the ErraiCommon module, we have disabled the built-in GWT log handlers and provided four handlers of our own:

  • ErraiSystemLogHandler : prints log statements to the terminal in Development Mode

  • ErraiConsoleLogHandler : prints statements to the web console in the browser

  • ErraiDevelopmentModeLogHandler : prints statements in the Development Mode window

  • ErraiFirebugLogHandler : prints statements to the console in Firefox
    These loggers are all enabled by default and set to handle all log levels.

Configuring Errai Client-Side Log Handlers

Log handler levels can be changed at run-time through Java or Javascript. To do so through Java, use the LoggingHandlerConfigurator in Errai Common. Here's an example:

import org.jboss.errai.common.client.logging.LoggingHandlerConfigurator;
import org.jboss.errai.common.client.logging.handlers.ErraiSystemLogHandler;
import java.util.logging.Level;

public class HandlerLevelAdjuster {

  public static void logAll() {
    LoggingHandlerConfigurator config = LoggingHandlerConfigurator.get();
    ErraiSystemLogHandler handler = config.getHandler(ErraiSystemLogHandler.class);

  public static void disableLogging() {
    LoggingHandlerConfigurator config = LoggingHandlerConfigurator.get();
    ErraiSystemLogHandler handler = config.getHandler(ErraiSystemLogHandler.class);


Each handler has a native Javascript variable associated with its log level:


Variable Name









Since these are native Javascript variables, they can easily be set in a script tag on your host page:

<script type="text/javascript">
  erraiSystemLoghandlerLevel = "INFO";

The possible log levels correspond to those in java.util.logging.Level.

Format String

The Errai log handlers use ErraiSimpleFormatter to format log output. The format string is similar to that used in by java.util.SimpleFormatter (for precise differences please see the javadocs for ErraiSimpleFormatter and StringFormat).

As with handler settings, these can be configured in Java or Javascript. To do so in Java, use ErraiSimpleFormmater.setSimpleFormatString(String). In Javascript, just set the variable erraiSimpleFormatString to the desired value.

Server-Side Setup

On the server you are free to use any logging back-end that has slf4j bindings (or to make your own). Just make sure to add dependencies for the slf4j-api artifact and the slf4j binding you choose. Note: Some application servers provide their own slf4j bindings (such as JBoss AS), in which case you should add your binding dependency as provided scope.

To learn more about how to setup slf4j for your server-side code, see their website.

Example Usage

Here is sample usage of the slf4j code (which with the above setup can be run on the client or server):

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;

public class LogExample {

  public void logStuff() {
    // Get a logger for this class
    @Inject Logger logger;

    // Logging going from most to least detailed
    logger.trace("this is extremely specific!");
    logger.debug("this is still pretty specific");
    logger.info("this is an average log message");
    logger.warn("there might be something fishy here...");
    logger.error("uh oh... abandon ship!", new Exception("I am a logged exception"));

Logger Names

By default, the above example with provide a logger with the fully qualified class name of the enclosing class. To inject a logger with an alternate name, use the NamedLogger annotation:

import org.slf4j.Logger;
import javax.inject.Inject;
import org.jboss.errai.common.client.api.NamedLogger;

public class NamedLogExample {

  // Get a logger with the name "Logger!"
  @Inject @NamedLogger("Logger!") logger;
  // Get the root logger
  @Inject @NamedLogger rootLogger;

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:34:58 UTC, last content change 2013-12-10 21:34:41 UTC.