Spring Boot Database Project Configuration for Logging Using AOP
In this article, see how to set up an asynchronous framework in a Spring Boot project for logging.
Join the DZone community and get the full member experience.Join For Free
In this article, I would like to show you how to set up an asynchronous framework in a Spring Boot project for logging. This is intended to be the first part of a series of three. The next article will involve configuring the application for analytics.
Why log asynchronously? The advantage is that logging is treated as a cross-cutting concern. This means that logging should never be entangled in the main application logic.
Also, when does logging become analytics? What is the correlation between logging and application analytics? We have debug logging to debug applications when finding the source of bugs. However, Info level logging can add value to analytics. Hence it can be seen that there is an overlap in logging and analytics. Machine learning could also ingest this data to analyze when failures occur as well as performance.
For an introduction to Spring AOP and the logging code that is referenced in this article, please see https://www.baeldung.com/spring-aop-annotation.
First, we set up our logging by creating a separate package for logging:
We have added the LoggingAOP class and the annotation interfaces LogExecutionTime and LogExecutioonDetails. These two interfaces will give you the execution time that a method takes and the method signature respectively.
In the MutlipleDatabaseController, add the annotation to methods, for example, when saving a new person:
When the application is run and we make a call we have:
[nio-8080-exec-1] com.gabriel.logging.LoggingAOP : Signature=Person com.gabriel.multipledatabaseconnection.controller.MultipleDatabaseController.savePerson(Person): Args= [Ljava.lang.Object;@518639d8: Executed in 100ms
Please note that the AOP is done in a clean manner without too much configuration in the POM. For example, I don't specify compile time or load weaving explicitly. I let Spring determine everything. Spring also picks the optimal AOP framework. This reduces the startup time of the application and leaves a cleaner POM with less configuration. Less is always better!
The source code for this article is under the logging branch in gabriel-multipledatabases.
Published at DZone with permission of Gabriel Campbell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.