We all say “WE NEED TO CHECK LOGS”
Adding Logging Mechanism To Your App Will Save You Hours, Let’s Do That!
Add Logging Mechanism to Spring Boot Rest API CRUD App with Log4j2
Hi guys! In this article we will add a logging mechanism to our Spring Boot application. Logging mechanism is a must for applications I think. When you are building your application, you can debug it, but when your application runs on the production, and interact with users, some problems may occur. You may missed a bug at the development and test, but in the production bug says “Hi!”, at this point with logging mechanism you can search for your logs and see what happened in the application yesterday at 3pm!
Let’s start! I am going to use my recent project for this, which is a CRUD app built with Spring Boot. Please see the article for this, also you can download the source code from Github.
How To Create CRUD Rest API App with Spring Boot
In this story, we will create CRUD REST API with Spring Boot!
In this app we have included Spring Boot Starter Web dependency. In this dependency Spring Boot has it’s own logging mechanism, while running our app at the console we are seeing some logs right? So I want more functionality, to do that I will exclude the default logging and after that let’s add “log4j2”.
To start log4j2, we need configuration file. At this configuration file we are going to define our log mechanism. So let’s create a log4j2.xml file under the resources folder.
We need to define what we want from log4j2 and define our log mechanism. Our log file will be at the same directory “.logs/crudApp.log”, you can change it from the .xml file. With pattern we are defining what should every log line looks like at which level. We can change the pattern as log level specific. We are defining only which log level we want to see. For here I want to see debug logs.
Another useful property from log4j2 is its policies. You can set a limitation to the size of the log file for example. While your application is working at the production, every user move will grow your log file. So it is a good idea to set limit to the size. When it reached the size, log file will be deleting the oldest lines and inserting new log lines. I am going to use that policy for sure.
It looks like we have finished editing our configuration file. So we can use our log mechanism. To do that, we need to add this line to beginning of the controller class that we want to add some logs, after that we can use it.
Let’s add basic log to the one of our methods in the controller class.
With this line if our project hit the line, thanks to log4j2 we will be storing the output inside the log file that we defined in the .xml file.
After edit, UserController class looks like this.
Let’s run the app and with postman send our app one GET Request. You can call it with clicking the link;
Don’t forget if you don’t have any user there, you might get an error since you don’t have any Users, but still it hits the log line and you will be see the line that we added with logger. Also notice that you will have NullPointerException and in the log file this exception will be stored as well. So that you can check what happened.
Let’s check the line.
2020–04–27 17:14:08,720 DEBUG c.a.c.d.c.UserController UserController — getUsers called!
As we can see, timestamp, level of log, class name and our log message is here. We can add extra variables to that string to make this log more useful. Also you can always change the pattern to reach better match for your purpose.
With this functionality, you can store your log and if something goes wrong you can check the file later to see what was wrong. Thanks to timestamp it will be really easy to detect what happened.
When you finish your app, it is a good idea to store some logs when it’s in the production, if somehow somebody tells you about any problem, guessing the problem causes is really hard, log file will be there for you to tell the story.
Congrats! We have added a logging mechanism to our Spring Boot RestAPI CRUD app. You can find the Github repository for the source code below. I am going to write new stories while continue with that application.
This repository contains source codes of my Medium articles about Spring Boot 1-HelloWorldApp -> Medium/How to Create…
That’s all for now. I hope that article will help you, if you have any questions or suggestions please leave me a comment. Thank you for your time. Hope to see you in next articles!