Logging with Logback configuration
Spring boot enables logback to be configured in a specific manner to meet our project’s requirements. In order to do so, we need to define a configuration file where we can specify logging pattern, color, different properties for file logging & console logging, and an efficient rolling policy to prevent the creation of huge log files. Whenever Spring boot finds a file with any of the following names, It automatically overrides the default configuration.
- logback-spring.groovy
- logback.groovy
- logback-spring.xml
- logback.xml
Now let’s create a simple logback-spring.xml file
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > <!-- Setting up log path and log file name --> < property name = "LOG_PATH" value = "./logs" /> < property name = "LOG_FILE_NAME" value = "application_logback" /> <!-- Setting up logging pattern for console logging --> < appender name = "ConsoleOutput" class = "ch.qos.logback.core.ConsoleAppender" > < layout class = "ch.qos.logback.classic.PatternLayout" > < Pattern > %white(%d{ISO8601}) %highlight(%-5level) [%yellow(%t)] : %msg%n%throwable </ Pattern > </ layout > </ appender > <!-- Setting the filename and logging pattern for log file --> < appender name = "LogFile" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file >${LOG_PATH}/${LOG_FILE_NAME}.log</ file > < encoder class = "ch.qos.logback.classic.encoder.PatternLayoutEncoder" > < Pattern >%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level : %msg%n </ Pattern > </ encoder > <!-- Setting up a rolling policy with rolling done daily and when file size is 10MB--> < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > < fileNamePattern >${LOG_PATH}/archived/${LOG_FILE_NAME}-%d{yyyy-MM-dd}.%i.log </ fileNamePattern > < timeBasedFileNamingAndTriggeringPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > < maxFileSize >10MB</ maxFileSize > </ timeBasedFileNamingAndTriggeringPolicy > </ rollingPolicy > </ appender > <!-- Logging at INFO level --> < root level = "info" > < appender-ref ref = "LogFile" /> < appender-ref ref = "ConsoleOutput" /> </ root > <!-- Logging at TRACE level --> < logger name = "com.log" level = "trace" additivity = "false" > < appender-ref ref = "LogFile" /> < appender-ref ref = "ConsoleOutput" /> </ logger > </ configuration > |
On running the application, and visiting the page http://localhost:8080/log, we receive the following console output:
The console output is as per the logging pattern and color defined in the logback-spring.xml file. A new log file by the name application-logback.log is created in the/logs folder present in the current path which gets archived through a rolling policy. The log file output is as follows:
Spring Boot – Logging
Logging in Spring Boot plays a vital role in Spring Boot applications for recording information, actions, and events within the app. It is also used for monitoring the performance of an application, understanding the behavior of the application, and recognizing the issues within the application. Spring Boot offers flexible logging capabilities by providing various logging frameworks and also provides ways to manage and configure the logs.
Why to use Spring Boot – Logging?
A good logging infrastructure is necessary for any software project as it not only helps in understanding what’s going on with the application but also to trace any unusual incident or error present in the project. This article covers several ways in which logging can be enabled in a spring boot project through easy and simple configurations. Let’s first do the initial setup to explore each option in more depth.