How to read WildFly logs using the CLI

In this short tutorial we will learn how to inspect the WildFly log files from the Command Line Interface.

One interesting feature is the ability to display log files using the management instruments. The advantage of this approach is that you can go through the server log files even if you are not remotely connected to the machine.

There are two main approaches to do that:

  • Query the logging subsystem to fetch the server logs
  • Use the HTTP Management API to fetch logs

Let’s see both options in detail.

How to read WildFly logs using the Command Line Interface

Gathering information from server log files is quite intuitive. Firstly, let’s learn how to list the available log files using the list-log-files command on the logging subsystem:

/subsystem=logging/:list-log-files
{
    "outcome" => "success",
    "result" => [
        {
            "file-name" => "server.log",
            "file-size" => 24564L,
            "last-modified-date" => "2022-11-20T09:37:26.931+0100"
        },
        {
            "file-name" => "server.log.2022-11-11",
            "file-size" => 120521L,
            "last-modified-date" => "2022-11-11T19:23:19.631+0100"
        },
. . . . .

Another interesting option for us, is the ability to display the actual log files content filtered by some parameters such as the number of lines to read (lines parameter) and the lines to skip (skip parameter) from the header. For example::

/subsystem=logging/:read-log-file(name=server.log,lines=-1,skip=0
{
    "outcome" => "success",
    "result" => [
        "2022-11-20 09:37:21,741 INFO  [org.jboss.modules] (main) JBoss Modules version 2.0.3.Final",
        "2022-11-20 09:37:22,174 INFO  [org.jboss.msc] (main) JBoss MSC version 1.4.13.Final",
        "2022-11-20 09:37:22,180 INFO  [org.jboss.threads] (main) JBoss Threads version 2.4.0.Final",
        "2022-11-20 09:37:22,272 INFO  [org.jboss.as] (MSC service thread 1-1) WFLYSRV0049: WildFly Full 27.0.0.Final (WildFly Core 19.0.0.Final) 
starting",
. . . .

Then, if you want to emulate Linux tail command, you can add the “tail” parameter which accepts a boolean. Here’s for example how to read the last 10 lines of the server.log. For example:

/subsystem=logging/:read-log-file(name=server.log,tail=true,lines=10,skip=0)
{
    "outcome" => "success",
    "result" => [
        "2022-11-20 09:37:26,896 INFO  [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report",
        "WFLYCTL0184:    New missing/unsatisfied dependencies:",
        "      service jboss.jdbc-driver.postgres (missing) dependents: [service jboss.driver-demander.java:/PostGreDS, service org.wildfly.data-s
ource.PostgrePool] ",
        "      service jboss.naming.context.java.jboss.DefaultJMSConnectionFactory (missing) dependents: [service jboss.naming.context.java.module
.helloworld-mdb.helloworld-mdb.DefaultJMSConnectionFactory] ",

Finally, another useful command is the read-boot-errors which will list all errors from the server start up in the response:

/core-service=management:read-boot-errors

How to download log files with the CLI

Additionally, it is worth mentioning that you can check your log files also through the attachment CLI command, which has options both for displaying files and save them locally.

Here is how to display the content of server.log file using the attachment command:

attachment display --operation=/subsystem=logging/log-file=server.log:read-resource(include-runtime)

Next, here is how to save the logs locally:

attachment save --operation=/subsystem=logging/log-file=server.log:read-resource(include-runtime) --file=./server.log

Reading WildFly logs using HTTP Management API

WildFly allows you to use the HTTP channel to stream server logs from a remote location. This can be useful if you want to perform some filtering/operations on the content of log files remotely. For example, you can test the following through the browser:

http://localhost:9990/management/subsystem/logging/log-file/server.log?operation=attribute&name=stream&useStreamAsResponse&user=admin&password=mYpassword1!

Replace the user and password attribute in the previous URL with a management user and password. You should expect your browser to request whether you are going to open the log file or save it locally.

A more programmatic approach consists in using a utility such as curl to request the log file and save it locally. This offers the benefit that you can choose the log format (in our example, JSON). For example:

curl --digest -L -D - http://127.0.0.1:9990/management?useStreamAsResponse \
--header "Content-Type: application/json" -u admin:mYpassword1! \
-d '{"operation":"read-attribute","address":[{"subsystem":"logging"},{"log-file":"server.log"}],"name":"stream"}'
Found the article helpful? if so please follow us on Socials