JBoss WildfFly Logging Configuration Example
1. Introduction
In this example we will review the WildFly
logging
subsystems. We will apply the configuration to a Web application to generate a separate log files for our application instead of writing it to default server log file.
1.1 WildFly
WildFly
is an application server written in Java
that implements Java EE
specification. It’s a fast and lightweight server. It’s built on a modular service container that enables services on demand when needed by the application. The latest release as of this this writing is 14.0.1, which is Java EE 8
certified.
1.2 WildFly Logging Configuration
Logging
subsystem represents the overall server logging
configuration. It is made up of the following four parts:
- Handler
- Logger
- Root Logger
- Logging Profiles
1.2.1 Handlers
Handlers
define how log messages are recorded. Following handlers
are available in WildFly
:
- async-handler – An async-handler is a
handler
that asynchronously writes log messages to it’s childhandlers
. This type ofhandler
is generally used to wrap otherhandlers
that take a substantial time to write messages. - console-handler – A console-handler is a
handler
that writes log messages to theconsole
. Generally this writes tostdout
, but can be set to write tostderr
. - custom-handler – A custom-handler allows you to define any
handler
as ahandler
that can be assigned to alogger
or aasync-handler
. - file-handler – A file-handler is a
handler
that writes log messages to the specified file. - periodic-rotating-file-handler – A periodic-rotating-file-handler is a
handler
that writes log messages to the specified file. The file rotates on the date pattern specified in the suffix attribute. The suffix must be a valid pattern recognized by thejava.text.SimpleDateFormat
and must not rotate on seconds or milliseconds. - periodic-size-rotating-file-handler – A periodic-size-rotating-file-handler is a
handler
that writes log messages to the specified file. The file rotates on the date pattern specified in the suffix attribute or the rotate-size attribute. The suffix must be a valid pattern recognized by the java.text.SimpleDateFormat and must not rotate on seconds or milliseconds. - size-rotating-file-handler – A size-rotating-file-handler is a
handler
that writes log messages to the specified file. The file rotates when the file size is greater than the rotate-size attribute. The rotated file will be kept and the index appended to the name moving previously rotated file indexes up by 1 until the max-backup-index is reached. Once the max-backup-index is reached, the indexed files will be overwritten. - socket-handler – A socket-handler is a
handler
which sends messages over asocket
. This can be aTCP
orUDP
socket
and must be defined in asocket
binding group under thelocal-destination-outbound-socket-binding
orremote-destination-outbound-socket-binding
resource. - syslog-handler – A syslog-handler is a
handler
that writes to asyslog
server viaUDP
. Thehandler
supportRFC3164
orRFC5424
formats.
1.2.2 Logger
Loggers
are used to log messages. A logger
is defined by a category generally consisting of a package name or a class name.
A logger
is the first step to determining if a messages should be logged or not. If a logger
is defined with a level, the level of the message must be greater than the level defined on the logger
. The filter is then checked next and the rules of the filter will determine whether or not the messages is said to be loggable.
A logger
has the following attributes:
- filter-spec – The filter-spec attribute is an expression based string to define filters for the
logger
. - #handlers – The
handlers
attribute is a list ofhandler
names that should be attached to the logger. If theuse-parent-handlers
attribute is set to true and the log messages is determined to be loggable, parentloggers
will continue to be processed. - #level – The level attribute allows the minimum level to allow messages to be logged at for the
logger
. - use-parent-handlers – The
use-parent-handlers
attribute is aboolean
attribute to determine whether or not parentloggers
should also process the log message.
1.2.3 Root Logger
The root logger
defines the level of messages to be logged. You can use this to limit the logging. It reference a handler
or set of handlers
. Each handler
in turn declares the log format and output:
1.2.4 Logging Profiles
Logging
profiles are like additional logging
subsystems. Each logging
profile consists of handler
configurations, logger
and the root logger
declarations.
You can assign a logging
profile to a deployment via the deployments manifest. Add a Logging-Profile entry to the MANIFEST.MF
file with a value of the logging
profile id. For example a logging profile defined on /subsystem=logging/logging-profile=demo
the MANIFEST.MF
would look like:
Manifest-Version: 1.0 Logging-Profile: demo
One logging
profile can be assigned to multiple deployments. Using a logging
profile allows you to make runtime changes to the configuration. This is an advantage over the per-deployment logging configuration as you won’t have to redeploy the application for logging
changes to take affect.
1.3 Logging Formatter
Logging
formatter is used to format the log messages. A formatter can be assigned to a handler
. WildFly
logging
subsystem includes following type of formatters:
- JSON Formatter – It’s used to format log messages in
JSON
- Pattern Formatter – It’s used to format log messages in plain text.
- XML Formatter – It’s used to format log messages in
XML
- Custom Formatter – Note that most log records are formatted in the
printf
format.
2. WildFly Logging Configuration Example
Now it’s time to apply what we have learnt so far in a real world example. We will configure logging for our Web Application
so that the log messages are written to our specified log file instead of default server log file. You can follow my previous article JBoss WildFly NetBeans Example to create a web application in NetBeans
and running it on WildFly
server or simply download the NetBeans
project to follow along this example.
2.1 Technologies used
For this example, we will use the following tools in a Windows
64-bit
platform:
- NetBeans – 8.2
- Java – 1.8.0_161
- WildFly – 14.0.1
2.2 Logging Configuration
The default log files for a standalone server can be found in the log subdirectory. The folder path is ./standalone/log/server.log
. The configuration files are in XML
format and are available in ./standalone/configuration
. As we are using the full standalone version of the server we are interested in standalone-full.xml
configuration file. Before making any changes make sure to take a back up of the file. Open the file in any text editor and add the following code to the logging
subsystem section.
Standalone-full.xml
<periodic-rotating-file-handler name="MY_HANDLER" autoflush="true"> <formatter> <named-formatter name="PATTERN"/> </formatter> <file relative-to="jboss.server.log.dir" path="jboss-wildfly-netbeans-example.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <logger category="com.jcg" use-parent-handlers="false"> <level name="INFO"/> <handlers> <handler name="MY_HANDLER"/> </handlers> </logger>
- Line 1: We are adding a
handler
;periodic-rotating-file-handler
to be specific with nameMY_HANDLER
- Line 2 – 4: We are using the
formatter
calledPATTERN
. It’s already defined in the configuration file. We are simply using it. - Line 5: This is where we specify the location of our log file. In this case, our log file will be placed in whichever folder is defined for the server log files.
- Line 6: The date will be suffixed to the file when it’s rotated
- Line 7: Flag to indicate that the date will be appended to the file name
- Line 9:
Logger
category is a . (dot) delimited string generally consisting of thepackage name
or aclass name
. In this case, thelogger
com.jcg
is the parent logger ofcom.jcg.wildflyexample
- Line 10: The level attribute allows the minimum level to allow messages to be logged at for the logger. In this case we are logging anything above
INFO
level - Line 11 – 13: Making sure the
logger
uses our handler calledMY_HANDLER
That’s all we need for now. Save the file.
2.3 Java Code Change
Let’s modify our Java
Bean
code so that the log messages are written to the new log file instead of default server log file. Following is the listing of our GreetingsBean.java
file. We will review only the new code.
GreetingsBean.java
package com.jcg.wildflyexample; import javax.inject.Named; import javax.enterprise.context.RequestScoped; import org.jboss.logging.Logger; /** * * @author Satya Choudhury */ @Named(value = "greetingsBean") @RequestScoped public class GreetingsBean { private String userName = ""; private static Logger log = Logger.getLogger(GreetingsBean.class.getName()); /** * Creates a new instance of GreetingsBean */ public GreetingsBean() { //System.out.println("Created GreetingsBean instance..."); log.info("Created GreetingsBean instance..."); } public String getUserName() { return this.userName.trim(); } public void setUserName(String userName) { this.userName = userName.trim(); } public String greetUser() { return "greeting"; } }
- Line 5: We imported the
WildFly
logger
package
- Line 16: We defined a
private
static
variable calledLog
, which is a handle to thelogger
- Line 23: We logged an informational message. There are methods available for
debug
,warn
, etc. I will encourage you to read the documentation.
We are ready to see the configuration in action so, save the file and run the application. NetBeans
will start the server and deploy the application. On the welcome page of our application, enter a name then press submit. Verify that the new log file is generated and log messages are written correctly. To do so, navigate to the log folder of the server. You should see the log file. In my case the log file is named jboss-wildfly-netbeans-example.log file.
3. JBoss WildfFly Logging Configuration – Summary
In this example, we reviewed the various attributes and options available for logging configuration of WildFly
server. We applied the configuration to our web application to generate separate log files instead of writing the log messages to default server log file.
4. Download the Source Code
This was an example of JBoss
WildFly
logging configuration.
You can download the full source code of this example: Download the NetBeans project
Thanks! Great tuto!!