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")