Skip to content

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:

Terminal window
INFO | jvm 1 | 2019/07/15 17:14:06 | 17:14:06.920 [Thread-3] INFO com.bswen.MyPool - MyPool-1 - Closed.

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:

application.properties
# Set logging level
logging.level.=WARN
logging.level.com.bswen=DEBUG
logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n

After making these changes, rerun your application. You should now see logs with line numbers, like this:

Terminal window
INFO | jvm 1 | 2019/07/15 17:14:48 | 17:14:48.048 [housekeeper] DEBUG com.bswen.MyPool:378 - MyPool-1 - After cleanup stats (total=1, active=0, idle=1, waiting=0)

4. Spring Boot Logging Properties

Spring Boot provides several logging pattern properties:

logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup.
logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup.

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!