1. Introduction

In this post, I would demo how to print or show line number of loggers in SpringBoot applications.

By default, SpringBoot does not print or show linenubmer of loggers, just like this:

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

How should we do when we want to print or show the logger’s linenubmer?

2. Environments

  • SpringBoot 1.x and 2.x

3. The solution

Change your SpringBoot’s application.properties file like this:

#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

And rerun your application, you would get this log:

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)

Pay attention to the %L of the logging.pattern.console, it means the linenumber of the logger.

4. The SpringBoot logging properties

There are some logging pattern properties in SpringBoot apps:

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.

The pattern is provided by logback.

5. LogBack pattern variables

By default, SpringBoot depends on the slf4j and logback, you can specify the logback layout patterns as follows:

  • L / line
    • Outputs the line number from where the logging request was issued.Generating the line number information is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.
  • t / thread
    • Outputs the name of the thread that generated the logging event.
  • n
    • Outputs the platform dependent line separator character or characters.This conversion word offers practically the same performance as using non-portable line separator strings such as “\n”, or “\r\n”. Thus, it is the preferred way of specifying a line separator.

6. Summary

If you want to specify the logging layout patterns in SpringBoot apps, you can specify the logging.pattern.console or logging.pattern.file in the application.properties, the pattern variables is provided by logback.