Using a Logger for a specific application (EAP 6 and WildFly)

A common requirement for many applications is to use a specific logger to trace log events. In this recipe we will learn how to use Logging profiles to define a logger that can be activated at application level. This tutorial can be used with JBoss EAP 6.2 (and higher) and WildFly application server.

Let’s start from a sample. Here we have defined a File size based Handler and a Logger for intercepting 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>

The problem with the above logger is that you might have lots of applications using the com.mycompany namespace, therefore it is not so useful as you might think. One solution is to use a Logger profile which defines a named profiles that has to be activated at application level. Let’s see the above 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