In this blog, we will learn about Logging in Spring Boot using Logback and Application Insight. We will use Aspect-oriented programming to enable logging out of the box for all modules in Mult-Module Spring Boot Project. Please check my GitHub for complete code.
Logging is one of the significant cross-cutting concern in Software Development. It helps in
Debugging any issues in Code.
Understanding how the system is performing.
Helps other developers to understand the flow of code.
Cloud-based Logging System can easily integrate with Spring Boot Logging Frameworks. (e.g. Microsoft Application Insight, Loggly, AWS Centralized Logging, Google Cloud Logging, Logstash, etc.). These logging resources helps developers to save their log in the cloud, and once data is in the cloud, we can easily use other cloud capabilities on these logs. In this implementation, we will be using Microsoft Application Insight as a Cloud-based logging service.
There are 2 different implementation
Using a multi-module project skeleton. ( For this implementation, I have created a custom parent and starter, that can be used instead of spring boot parent. Anyone can clone my repo, and get started with it.)
Doing everything by yourself. ( For this implementation, I will provide the steps, that can be used to implement logging by yourself.)
Both implementations will perform the same job, but 1st implementation is an easy and efficient way. It has many different functionality, that can utilized in service, which will be discussed in other parts of this series.
Both Implementation logs following properties, apart from default properties:
stack_trace in case of errors.
Implementation1: Using a multi-module project skeleton
In order to get started with it, check out my first blog in this series, and look for How to Use Section.
You can also checkout my GitHub Documentation, it also has steps, to create new service, and once service is created, then you can follow below steps.
This implementation will perform out of the box logging for Service, Controller, Repository and Components. For other classes, it has @Loggable annotation.
Check out my Sample Service implementation on GitHub.
This implementation has only 2 steps:
Step1: Use below pom.xml
Step 2: Add Below content in the application.yaml
How to use @Loggable annotation
Implementation2: Developing everything by your self.
This implementation assumes you already have existing Spring Boot Service.
This implementation is a 4 step process.
Create classes to enable out of the box logging for classes annotated with @Component, @Service, @Repository, @Controller. And for all other classes, create @Loggable annotation.
if you want to log some additional properties, you can use MDC.put(“property_name”, “property_value”);
if you want to log some property during the start of every request like requested, then extend OncePerRequestFilter, and then use MDC to set property
Step1: Add below dependencies in your pom.xml
Step 2: Add logback-spring.xml file under resources folder
This will help to send logback log to Application Insight.
Step 3: Add below content in the application.yml file
Step 4: Add below classes
In this implementation, we will enable out of the box logging for Controllers, Service, Repository and Component Classes, and for other classes, we will create a custom annotation, that will do the logging. This has one dependency on a different module from my multi-module project. However, you can easily use this without that dependency. Check my comments in a gist for standalone usage.
For all Other Classes:
I hope this implementation will help you in improving logging in your software.
Comments and feedback are most welcomed.
Thanks for reading. Happy Learning 😊