Configuring Logging for your Application

In this guide, you will learn how to use logging in your application and configure WildFly to display the logs at the level you want.

Prerequisites

To complete this guide, you need:

  • Roughly 10 minutes

  • JDK 11+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.9+

WildFly supports all major logging facades. These include:

Choose the logging facade you’d like to use and ensure it’s added in your Maven pom.xml file with a scope of provided. Then, we will add a few logs in our code at different levels. Finally, we will configure WildFly to change the log level of our application logs independently of WildFly own logs.

In our example we will be using JBoss Logging.

Add a Dependency on JBoss Logging

In order to use JBoss Logging in our application, we need to add a dependency on it in the pom.xml.

The dependency is defined as:

<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <!-- Provided by WildFly -->
    <scope>provided</scope>
</dependency>

You need to add it to the <dependencies> section of the pom.xml:

<dependencies>
    ...
    <!-- copy the dependency here -->
</dependencies>

Add Logging to the Application

As an example, you can update the GettingStartedService.java file to add logging to the application at different levels:

package org.wildfly.examples;

import jakarta.enterprise.context.ApplicationScoped;
import org.jboss.logging.Logger;

@ApplicationScoped
public class GettingStartedService {

    private static Logger log = Logger.getLogger(GettingStartedService.class);

    public String hello(String name) {
        log.tracef("called method with: %s", name);

        String out = String.format("Hello '%s'.", name);
        log.infof("returning: %s", out);
        return out;
    }
}

You added a log Logger that can log messages with a name corresponding to the class org.wildfly.examples.GettingStartedService. You also added two logging calls, one at the TRACE level and the other one at the INFO level.

If you run the integration tests with mvn clean verify, you will only see the logs at the INFO level in the standard output:

$ mvn clean verify
...
15:18:08,482 INFO  [org.wildfly.examples.GettingStartedService] (default task-1) returning: Hello 'World'.
...

By default, WildFly only displays INFO logs on the console. Let’s now configure WildFly to display our application logs at the TRACE level.

Configure Logging in the WildFly Subsystem

The configuration of logging is done by WildFly in its logging subsystem. There are different ways to modify the WildFly configuration but as we used the wildfly-maven-plugin to provision WildFly, we can pass a CLI script so the WildFly configuration will be modified whenever we invoke mvn package.

First, we add a configuration.cli in the src/main/scripts directory:

# Start the embedded server
embed-server
# let the console display TRACE logs
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=level, value=TRACE)
# create the logger for our code (with the name org.wildfly.examples corresponding to our package)
/subsystem=logging/logger=org.wildfly.examples:add(level=TRACE)
stop-embedded-server
Note
We use the logger name org.wildfly.examples instead of the full logger name. This will allow trace logs from all loggers which have this same base name.

This script contains the management operations to change the WildFly configuration. We could invoke any management operations but, in this case, we only modify the /subsystem=logging resources that control the logging aspects.

You then need to modify the wildfly-maven-plugin configuration in pom.xml to execute this:

Copy the XML snippet:

<!-- Execute the script in offline mode -->
<offline>true</offline>
<scripts>
    <script>${project.build.scriptSourceDirectory}/configuration.cli</script>
</scripts>

And add it to the <configuration> section of the wildfly-maven-plugin:

<plugin>
    <groupId>org.wildfly.plugins</groupId>
    <artifactId>wildfly-maven-plugin</artifactId>
    <version>${version.wildfly.maven.plugin}</version>
    <configuration>
        <!-- copy the XML snippet here -->
    </configuration>
    <executions>
        <execution>
            <id>provision-server</id>
            <phase>package</phase>
            <goals>
                <goal>provision</goal>
                <goal>execution-commands</goal>
            </goals>
        </execution>
    </executions>
</plugin>

With that change in place, if you provision WildFly and run the integration tests again, both log calls are now displayed:

$ mvn clean verify

...
16:04:22,260 TRACE [org.wildfly.examples.GettingStartedService] (default task-2) called method with: World
16:04:22,260 INFO  [org.wildfly.examples.GettingStartedService] (default task-2) returning: Hello 'World'.
...

What’s next?

WildFly provides extensive logging configuration. You can learn more by reading WildFly’s Logging Subsystem Configuration Guide and its model reference.

< Back to Guides