Make Logging Easy In Spring Boot
Posted By : Gaurav Kumar | 22-Jan-2021
Hello geeks, here I came up with another interesting blog on Logger in spring boot. To understand the logger we need their requirement first.
In our application, there are some situations, where our code breaks or behaves abnormally, to understand this we need some of the traces of code. Mean from where and why this abnormal or code break happens. So, we need to track or print out the class name, method name with the line number from where the issue occurred.
So, all in all, we just need a console output mentioning the exact issue with class level and method level details with line number. We can use a simple system.out.println but it has no flexibility and configuration on the application level, Like filtering the log level, filtering the packaging, etc.
The logging facilities are there on JDK since 1.0 under java.utils.logging package in Logger class. We get the hold of this static class's object using Logger.getLogger(ClassName).
Log level
- Info- - for info messages
- Severe -- for severe messages
- Fine -- for fine debugged message
Also Read: Creating Asynchronous Methods in Spring Boot
Handlers
- File Handler -- logging into the file
- Console Handler -- logging to console
The issue with JSK 1.0 Logging
- Performance Issues
- Not flexible
- Needed More Features
So, we need a framework or plugin for proper control and configuration of logging. Some of the popular logging frameworks are LOG4J, SLF4J, Logback, and LOG4J2.
Log4j is one of the earlier frameworks in java for logging. In the same fashion, we can get hold of the Logger class. and different logging levels are-
- logger.debug()
- logger.error()
- logger.info()
It supports a lot of handlers like
- File log handler
- Console log handler
- Rolling File (Daily) log handler
- JDBC log handler
- SMTP log handler
- JMS log handler
- Formatting Options
At the same time when LOG4J is becoming popular among developers, there are other logging frameworks are introducing into the market and they are not compatible with each other.
Also Read: Upgrading Spring Boot To The 2.3.4 Release
So people moving towards People move towards facade libraries like:-
- SLF4J
- APACHE COMMONS
To overcome this issue SLF4J was introduced, which is actually consists of interfaces mentioning the standards of logging, and other logging frameworks implement those standards to make things in action. SLF4J is that much popular that other logging frameworks came up with their own standards similar to SLF4J, but they didn't get popularity.
Today's two popular logging frameworks are
- Logback
- Log4J2
Both frameworks are the successor of LOG4J.
Logback is the recommended way since it is developed by the same guy who develops the LOG4J and SLF4J.
LOG4J2 has it's own features like -
- Lazyloading
- Asyn logging
Logging level priority
Trace < Debug < Info < Warn < Error < Fatal.
Our spring boot application is already coming up with logger dependency added to it if it has spring-boot-starter-web dependency, which implicitly added dependency of spring-boot-starter-logging which is dependent on spring-jcl (spring common logging bridge) for logging stuff.
We can get hold of Logger by below code snippet.
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
Also Read: Spring Boot Application Configured As Eureka Server
The default log message format is as follows:-
- Date and Time – Millisecond precision and easily sortable.
- Log Level – ERROR, WARN, INFO, DEBUG or TRACE.
- Process ID.
- A “—” separator to distinguish the start of actual log messages.
- Thread – Enclosed in square brackets (maybe truncated for console output).
- Logger – This is usually the source class name (often abbreviated).
- The log message.'
Default log message color is-
- FATAL, ERROR -- Red
- WARN --Yello--
- INFO, DEBUG, TRACE -- Green
You can filter the logs based on packages filter and level either using application properties or yml file.
logging.level.root=WARN
logging.level.my.package=DEBUG
logging.file = main.log
logging.path = log
logging.file is the relative file name in which logs are being logged and the logging.path is the relative path to that file.
That's all about logging in to spring boot, hope you guys feel this blog informative. Thanks.
Choose Oodles For SaaS App Development Services
We are a 360-degree SaaS app development company that provides end-to-end software development services with a focus on next-gen technologies. Our development team specializes in using JavaScript technologies like Angular, Node, and ReactJS to build scalable, responsive, and feature-rich web applications.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gaurav Kumar
He is always ready to grasp new tech and tools as per project requirement.