How to configure Logging Profiles in WildFly

A logging profile allows you to define and configure logging specific to that particular application. This includes specifying the log levels, log handlers, and formatting for the logs generated by your application. In this article we will learn how to define a Logging Profile and the key differences with Standard Loggers.

Application specific logging

A common requirement for many applications is to use a specific logger to trace log events. The most obvious way to accomplish this requirement is to define a logger for a specific application namespace.

For example, here we are defining a File size based Handler and a Logger for the namespace “com.mycompany”.  This configuration allows to intercept the Handler:

<size-rotating-file-handler name="SIZE" autoflush="true">
	<level name="INFO"/>
	<file relative-to="jboss.server.log.dir" path="mylog.log"/>
	<append value="true"/>
</size-rotating-file-handler>
 
<logger category="com.mycompany" use-parent-handlers="false">
	<level name="INFO"/>
	<handlers>
		<handler name="SIZE"/>
	</handlers>
</logger>

What is the effect of this approach ?

  • Server-Wide impact: When defining a logger at the configuration level in WildFly, it affects the entire server environment.

  • Namespace overlap: Setting loggers at the server configuration level can influence multiple applications using that namespace.

Using Logger Profiles

By setting a logging profile for your application, you can isolate the logging configurations and preferences from the global or server-wide logging settings. This means you have control over how logs are managed and presented within your application’s context without affecting other applications running on the server.

Let’s see the initial example rewritten:

<subsystem xmlns="urn:jboss:domain:logging:1.2">  
  ........  
  <logging-profiles>  
    <logging-profile name="myapp1">  
		<size-rotating-file-handler name="SIZE" autoflush="true">
				<level name="INFO"/>
				<file relative-to="jboss.server.log.dir" path="mylog.log"/>
				<append value="true"/>
		</size-rotating-file-handler>
		<logger category="com.mycompany" use-parent-handlers="false">
			<level name="INFO"/>
			<handlers>
				<handler name="SIZE"/>
			</handlers>
		</logger> 
    </logging-profile>  
  </logging-profiles>  
</subsystem> 

Now the handler and its Logger are wrapped by the loggin profile named “myapp1”. The last step will be including in the MANIFEST.MF file of your application a reference to the Logging profile so that it can be switched on by your application:

Manifest-Version: 1.0  
Class-Path:  
Logging-Profile: myapp1  

Adding a Logging Profile using the CLI

Finally, we will show how to configure the Logging Profile using the Command Line Interface:

/subsystem=logging/logging-profile=myapp1:add()
/subsystem=logging/logging-profile=myapp1/size-rotating-file-handler=SIZE:add(append=true,autoflush=true,file={relative-to => jboss.server.log.dir,path => mylog.log},level=INFO)
/subsystem=logging/logging-profile=myapp1/logger=com.mycompany:add(category=com.mycompany,handlers=[SIZE],level=INFO,use-parent-handlers=false)

The above command will generate the same XML configuration from our example. On the other hand, if you want to attach your Logging Profile to the Root Logger (instead of using a custom Logger), then you can modify the script as follows:

/subsystem=logging/logging-profile=myapp1:add
/subsystem=logging/logging-profile=myapp1/file-handler=myHandler:add(level=ALL,file={"relative-to" => "jboss.server.log.dir","path" => "myapp.log"})
/subsystem=logging/logging-profile=myapp1/root-logger=ROOT:add
/subsystem=logging/logging-profile=myapp1/root-logger=ROOT:add-handler(name=myHandler)

Setting the Logging Profile in the Manifest using Maven

When using Maven to build your project, it is often convenient to define the Manifest File content from within the Maven plugins. For example, if you are building a web application then you could use the maven-war-plugin as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.4.0</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Logging-Profile>myapp1</Logging-Profile>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Conclusion

In conclusion, choosing between an application-specific logging profile and configuring loggers at the server level depends on the scope and impact you want the logging configurations to have: whether they need to be application-specific or server-wide. The decision often revolves around the level of control, isolation, and uniformity required in your logging practices within the WildFly server environment.

Found the article helpful? if so please follow us on Socials