Chapter 7. Accessing logs on your Thorntail application

7.1. Enabling logging

Each Thorntail fraction is dependent on the Logging fraction, which means that if you use any Thorntail fraction in your application, logging is automatically enabled on the INFO level and higher. If you want to enable logging explicitly, add the Logging fraction to the POM file of your application.

Prerequisites

  • A Maven-based application

Procedure

  1. Find the <dependencies> section in the pom.xml file of your application. Verify it contains the following coordinates. If it does not, add them.

    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>logging</artifactId>
    </dependency>
  2. If you want to log messages of a level other than INFO, launch the application while specifying the thorntail.logging system property:

    $ mvn thorntail:run -Dthorntail.logging=FINE

    See the org.wildfly.swarm.config.logging.Level class for the list of available levels.

7.2. Logging to a file

In addition to the console logging, you can save the logs of your application in a file. Typically, deployments use rotating logs to save disk space.

In Thorntail, logging is configured using system properties. Even though it is possible to use the -Dproperty=value syntax when launching your application, it is strongly recommended to configure file logging using the YAML profile files.

Prerequisites

Procedure

  1. Open a YAML profile file of your choice. If you do not know which one to use, open project-defaults.yml in the src/main/resources directory in your application sources. In the YAML file, add the following section:

    thorntail:
      logging:
  2. Configure a formatter (optional). The following formatters are configured by default:

    PATTERN
    Useful for logging into a file.
    COLOR_PATTERN
    Color output. Useful for logging to the console.

    To configure a custom formatter, add a new formatter with a pattern of your choice in the logging section. In this example, it is called LOG_FORMATTER:

    pattern-formatters:
      LOG_FORMATTER:
        pattern: "%p [%c] %s%e%n"
  3. Configure a file handler to use with the loggers. This example shows the configuration of a periodic rotating file handler. Under logging, add a periodic-rotating-file-handlers section with a new handler.

    periodic-rotating-file-handlers:
      FILE:
        file:
          path: target/MY_APP_NAME.log
        suffix: .yyyy-MM-dd
        named-formatter: LOG_FORMATTER
        level: INFO

    Here, a new handler named FILE is created, logging events of the INFO level and higher. It logs in the target directory, and each log file is named MY_APP_NAME.log with the suffix .yyyy-MM-dd. Thorntail automatically parses the log rotation period from the suffix, so ensure you use a format compatible with the java.text.SimpleDateFormat class.

  4. Configure the root logger.

    The root logger is by default configured to use the CONSOLE handler only. Under logging, add a root-logger section with the handlers you wish to use:

    root-logger:
      handlers:
      - CONSOLE
      - FILE

    Here, the FILE handler from the previous step is used, along with the default console handler.

Below, you can see the complete logging configuration section:

The logging section in a YAML configuration profile

thorntail:
  logging:
    pattern-formatters:
      LOG_FORMATTER:
        pattern: "CUSTOM LOG FORMAT %p [%c] %s%e%n"
    periodic-rotating-file-handlers:
      FILE:
        file:
          path: path/to/your/file.log
        suffix: .yyyy-MM-dd
        named-formatter: LOG_FORMATTER
    root-logger:
      handlers:
      - CONSOLE
      - FILE