Part I: Logging in Spring Boot in Multi-Module Project


spring boot logging with application insight and logback

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.


Introduction

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.


Spring Boot utilises SLF4J API for providing the abstraction to various Logging Framework ( e.g. Logback, Log4j, etc. ). Developers can pick their implementation of Logging Framework.

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.


Implementation

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:

  • method_name

  • class_name

  • target_class

  • message

  • parameters

  • stack_trace in case of errors.

  • execution_time_of_method_in_ms


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.

  • Update pom.xml

  • Update application.yml

  • Create logback-spring.xml

  • 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 Components:



  • For Service:




  • For Repository:



  • For Controller:


  • For all Other Classes:


I hope this implementation will help you in improving logging in your software.


Comments and feedback are most welcomed.


Please follow me on Linkedin, Github, and join our newsletter to keep yourself updated.


Thanks for reading. Happy Learning 😊

844 views

Recent Posts

See All