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
Find the
<dependencies>
section in thepom.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>
If you want to log messages of a level other than
INFO
, launch the application while specifying thethorntail.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
- A Maven-based application with the logging fraction enabled. For more information, see Section 7.1, “Enabling logging”.
- A writable directory on your file system.
Procedure
Open a YAML profile file of your choice. If you do not know which one to use, open
project-defaults.yml
in thesrc/main/resources
directory in your application sources. In the YAML file, add the following section:thorntail: logging:
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 calledLOG_FORMATTER
:pattern-formatters: LOG_FORMATTER: pattern: "%p [%c] %s%e%n"
Configure a file handler to use with the loggers. This example shows the configuration of a periodic rotating file handler. Under
logging
, add aperiodic-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 theINFO
level and higher. It logs in thetarget
directory, and each log file is namedMY_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 thejava.text.SimpleDateFormat
class.Configure the root logger.
The root logger is by default configured to use the
CONSOLE
handler only. Underlogging
, add aroot-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