Empowering Businesses with Tech
Logging using sl4j
In this section, we will learn about:
​
-
Terms associated with Logging using SLF4J
-
Implementing SLF4j for logging
-
Log levels
-
Configuring log level
Terms associated with Logging using SLF4J
a) Logging:
Logging means printing a message in our logs which we see in our console.
​
b) SLF4J:
Simple Logging Facade for Java is an interface that uses different logging libraries for logging. The actual logging is done by logging libraries, not by SLF4J.
​
c) LogBack:
LogBack is the default logging framework used in Spring Boot by SLF4J. This can be used in our application by using “spring-boot-starter-logging”, which comes by default when we use “spring-boot-starter-web”.
Implementing SLF4J for logging
-
We will use the “@SLF4J” annotation above our class name (JwtController).
-
And in our authenticate method we will use “log.info()” and give our string to be printed in the console when this method would be used.
Log Levels
Each log message has an associated log level that gives an idea about the importance and urgency of the message.
There are several log levels that decide the importance of a log message. The most important ones are logged first and the least important ones last.
​
Some log levels are:
-
TRACE: Trace level captures and prints all the details about every part of the application. From logs of Spring Boot to APIs, everything.
-
DEBUG: Debug is used when we are giving information in a very detailed manner.
-
INFO: Info is the default log level. It just shows a message about what is happening in the part.
-
WARN: Warn is used to show a message warning the user. It is used when you are not sure about what is exactly going to happen next and warn the user.
-
ERROR: Error is used to show a message that there is some error in the part of the application. This error will allow the application to run but maybe you can’t access certain services.
-
FATAL: Fatal is used to show the user a message that the application is going to stop. Some huge error has been committed and the application will abort.
-
ALL: All, has the lowest log level. It logs everything and includes custom logging levels as well. It is the combination of all other logging levels.
-
OFF: Off has the highest log level. Off is used to log nothing on the console.
Order of log levels and ranking from lowest to highest:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
Example: When you run “getLogs()” method, the related logs will be shown in the console. Here, you can see different log levels used.
Configuring Log Level
By default log level is set to INFO. So, if we use log levels higher than INFO i.e. warn, error, fatal, and off, it will work. But, if we try to use log levels that are lower than INFO then it will not work or not show logs.
​
To configure the log level we need to set the log level in our “application.yml”.
-
To use every log in your application you should set the lowest log level (TRACE). But if you used TRACE as a log level for the application then it will give every log about your application and starting the application would take time.
-
So, you can set the log level for the specific package in your application too.
-
For using a log level for an entire application, we use:
-
And for a specific package, we use: