How to create a Logger from WildFly CLI

The following sample script can be added to  your CLI scratchbook when you need to add a Logger on the fly for one package contained in your application.

GOAL: create a custom logger for the package classes named com.sample. The logging information will be written in a custom File Handler that writes information in the file jboss.server.log.dir/customfile.log

In order to do that, we will at first:

  • Create a new logger “com.sample”
  • Define its logging level
  • Create the CustomFileHandler
  • Assign the CustomFileHandler the the “com.sample” logger
connect
batch
/subsystem=logging/logger=com.sample:add   
/subsystem=logging/logger=com.sample:write-attribute(name="level", value="DEBUG")
/subsystem=logging/file-handler=CustomFileHandler:add(file={"path"=>"customfile.log","relative-to"=>"jboss.server.log.dir"})
/subsystem=logging/logger=com.sample:assign-handler(name="CustomFileHandler")

run-batch
:reload

Additionally, you might choose to re-use or not the root handler for your CustomFileHandler. With the following code we are not going to use the root handler:

/subsystem=logging/logger=com.sample:write-attribute(name="use-parent-handlers", value="false")

Recommended Articles

Using WildFly CLI Property Files for Variables - A Comprehensive Guide

Learn how to define and manage variables using a property file with WildFly CLI scripts. #WildFlyCLI #Java #CloudNative

Master WildFly CLI: Read and Manage Configuration Files with Ease

Learn how to read and manipulate WildFly configuration files using the WildFly CLI. Get started with simple commands like :read-config-as-xml and explore more advanced techniques.

Master WildFly CLI Variables with Examples and Hacks

Learn how to use variables in WildFly CLI, including setting multiple variables, replicating scripts, and making them persistent. #WildFlyCLI #Java #CloudNative

Simplify WildFly Management with Graphical CLI Interface

Learn how to use WildFly Command Line Interface (CLI) in graphical mode for easy application server management. #WildFly #Java #CloudNative