How to show or print line number of loggers in springboot applications
1. Introduction
In this post, I will demonstrate how to print or show line numbers of loggers in Spring Boot applications.
By default, Spring Boot does not print or show line numbers of loggers, as shown below:
How can we resolve this and print or show the logger’s line number?
2. Environments
- Spring Boot 1.x and 2.x
3. The Solution
To resolve this, modify your Spring Boot application.properties
file as follows:
After making these changes, rerun your application. You should now see logs with line numbers, like this:
4. Spring Boot Logging Properties
Spring Boot provides several logging pattern properties:
These patterns are provided by Logback.
5. Logback Pattern Variables
By default, Spring Boot uses SLF4J and Logback. You can specify Logback layout patterns as follows:
- L / line: Outputs the line number from where the logging request was issued. Generating line number information can be slow, so use it only when execution speed is not a concern.
- t / thread: Outputs the name of the thread that generated the logging event.
- n: Outputs the platform-dependent line separator character(s). This is the preferred way to specify a line separator.
6. Summary
If you want to customize logging layout patterns in Spring Boot applications, you can configure the logging.pattern.console
or logging.pattern.file
properties in the application.properties
file. These patterns are provided by Logback, and you can use variables like %L
to include line numbers in your logs.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!