Billing System using Spring Boot — logging framework integration using logback

Suresh
4 min readOct 22, 2020

--

The logging mechanism is plays important role in the part of Project Development. it is for………
Tracing the application activities.
Debugging the issues.
We can check application behavior.
We can verify that how long take particular flow execution.
Without a logging framework, it is very difficult to maintain the Production environment.
Please watch this full video Tutorial for Spring boot Logback integration
https://youtu.be/RVd9uuYd-jk

I have integrated a logging framework in the Billing Application. Integrating the logging framework is very easy in Spring Boot Application. Spring uses the logback for the logging mechanism. Let see how to integrate the logging framework.

I have created a logback-spring.xml file in the resource folder. We will explore each section in the logback-spring.xml file.

The following line explains that I want to create a log file mentioned in the configuration. The log file creates in the root directory of the billing Project.

<property name="LOGS" value="./logs" />

Following is the Console appender section, there are many appenders available. As of now, I am using Console and RolingFile appenders. Console appenders nothing but the log information print in application console i.e It prints in the server console. In the eclipse and Intellij IDE, you can see those logs in the console window.

<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>

</layout>
</appender>

In the above section, ConsoleAppender and PatternLayout are logback API. The ConsoleAppender for emitting logs in Server Console. The PatternLayout is for how do you want to print the log statements in the Server Console.

Inside the <Pattern> Tag I have mentioned the format of the log statement that I want to print on the Server Console. Let see one by one.

%d = Using this we can mention the date format in the log. each log print with current date and time, we are formatting that date and time using %d
%thread = Logging framework print the current thread name which printing the log statement
%-5level = This says that what is the level of log data.
%logger = This prints the current class name.
%msg%n = This prints whatever message we are passing inside the log function that could be log.info(“Test”) or log.debug(“hello”). The %msg% is “Test” and “hello” %n is a new line after printing particular log data.
This is the log pattern I am using for printing in the console window. For a clear understanding, please take a look at the pictorial diagram

Logging format

The below appender is RollingFile Appender. This Appender for logging into a file and creating a new file, based on the condition we have defined in the Rolling Policy in the below configuration.

<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/billing-app-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!-- date,priority,class,thread, message, newline -->
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>

</encoder>

<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 5 MegaBytes -->
<!-- %- increment the log files to 1.2...n -->
<fileNamePattern>${LOGS}/archived/billing-app-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

I am using a different log pattern in the Rolling configuration. Now we will check one by one.

<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>

%d = It prints the current date and time
%C = It prints the current class name
%t = It prints current Thread name
%m = it prints a message that we are passing in the log function.
%n = It creates a new line.

Next, we walk through the RollingPolicy section. Using this configuration I am saying that if the file size reaches 5MB then, the new file should create and the old file renamed to the Date pattern which I mentioned in <fileNamePattern>tag and the meaning of %i, the old file end with a number(index). For example, if the file reaches with 5MB a new file creates and the system logs the data into a new file, the old file renames like below
billing-<app-current-date>.<0> = this number is %i and it called as index
billing-<app-current-date>.<1>
billing-<app-current-dast>.<2>
billing-<app-current-dast>.<n>

Using filename pattern we can tell that I want to create a file with the current date and time

<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 5 MegaBytes -->
<!-- %- increment the log files to 1.2...n -->
<fileNamePattern>${LOGS}/archived/billing-app-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

Like the above configuration, we can configure different log configuration. As of now, I want to go with this. The below section says that I want to print all logs in the console which is coming from various parts of the Spring App like hibernate, spring MVC, etc. The Console configuration I have given root level which prints everything from the Application.

<root level="info">
<appender-ref ref="Console" />
</root

The below section configured specifically for log coming from the com.itgarden package. I want to print those logs in the Server console as well as in the file

<logger  name="com.itgarden" level="info" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>

This is my simple log configuration. I will update the log file configuration as I needed in the future. I hope this Story helpful for who wanted to know about logback configuration in the Spring Boot.
Watch this video for logback integration for Billing System.
https://youtu.be/RVd9uuYd-jk

Subscribe the YouTube channel for regular Billing System Project
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA
Take the update code for Billing System @ following GitHub Repository
https://github.com/sureshstalin/billingsystem

--

--