preface

I used to switch the logging framework to Log4j2 when I used SpringBoot, either because I thought it was reliable or because I was too old to do anything new. When I was working on a new project today, I was thinking that every time I had to do this migration, I would like to see how the Logback framework, which SpringBoot has always integrated by default (recommended), looked like.

Note: This article only covers the integration of SpringBoot and Logback. For details about Logback, please refer to the official website.

Log format

Take a line of logs about the SpringBoot application as an example:

The 2021-12-08 10:21:45. 28453-585 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat initialized with port(s): 8080 (http)Copy the code

A log consists of the following eight parts:

  1. Date and time, to the millisecond
The 2021-12-08 10:21:45. 585Copy the code
  1. The level of logging

INFO

Copy the code

Log levels of Logback include ERROR, WARN, INFO, DEBUG, and TRACE. Note that Logback does not support FATAL and is mapped to ERROR.

  1. The process ID

28453

Copy the code

Java (SpringBoot) application process ID.

  1. Dividing line

---

Copy the code

It is used only as a line separating the log header (1, 2, 3) from the log body (5, 6, 7).

  1. Name of the thread

[ main]

Copy the code

The string inside the square brackets ([]) is the thread name.

  1. Logger name

o.s.b.w.embedded.tomcat.TomcatWebServer

Copy the code

Logger name, usually replaced by the class name.

  1. The colon

:

Copy the code

Serves only as a separator between logger (6) and log message (7) and has no practical meaning.

  1. Log message

Tomcat initialized with port(s): 8080 (http)

Copy the code

Log message content.

Log output

By default, SpringBoot only outputs logs to the Console. If you want to output logs to a log file, you need to enable the log file output function by configuring properties logging.file.name or logging.file.path.

You can choose either of the two configuration attributes. In this article, logging.file.name is used as an example:


logging:

file:

name: /tmp/boot.log

Copy the code

When logs are output to the console, they are also output to the/TMP /boot.log file. When the size of the log file reaches 10 MB, log rotation occurs:

Boot.log boot.log.2021-12-08.0. Gz boot.log.2021-12-08.1. Gz boot.log.2021-12-08.2Copy the code

Logs are compressed by day (date, for example, 2021-12-08) in GZ format. Considering that there may be multiple archive files within a day, the compressed archive file name contains the order number (for example, 0, 1, 2, and 3).

Log rotation

We can also customize the log rotation process by configuring properties:


logging:

file:

name: /tmp/boot.log

logback:

rollingpolicy:

file-name-pattern: /tmp/boot-%d{yyyy-MM-dd}.%i.log

clean-history-on-start: false

max-file-size: 1GB

total-size-cap: 10GB

max-history: 7

Copy the code

Log rotation using the bottom Logback SizeAndTimeBasedRollingPolicy, at the same time support in accordance with the time log and log size to be archived.

  • logging.logback.rollingpolicy.file-name-pattern

The archive file name mode is based on which rules are used to generate the archive file name. The file name can contain the time and serial number.

Time, archive by log time, using %d{… } Specifies the date and time format (Java SimpleDateFormat), yyyY-MM-DD indicates archive by day (date) (archive by log time);

Serial number: indicates the serial number of multiple archive files generated in the same archive time because the log size reaches the threshold.

Log boot-2021-12-08.0.log boot-2021-12-08.1.log boot-2021-12-08.2.log boot-2021-12-08.3.log......Copy the code
  • logging.logback.rollingpolicy.max-file-size

Maximum log file size: KB/MB/GB/… To specify the unit; If the size of a log file exceeds the maximum value, logs are archived (by log size).

  • logging.logback.rollingpolicy.total-size-cap

Total size of log files (log files + archive files), using KB/MB/GB/… To specify the unit; When the total log file size exceeds the set value, old (old) archive files are deleted until the total log file size is less than the set value.

  • logging.logback.rollingpolicy.max-history

Retention period of archive log files. The archive files whose retention period exceeds the retention period will be deleted.

  • logging.logback.rollingpolicy.clean-history-on-start

Archive deletion is performed during log output. This property specifies whether an archive deletion (due to log time or size) is performed when the application is started.

The level of logging

Loggers are hierarchical, and we can specify different log output levels for loggers at different levels.

Specify the root logger log output level:


logging:

level:

root: info

Copy the code

As mentioned above, logger names are usually class names, and we can think of the logger hierarchy as being designed according to the class name structure:

Parent package name + several child package names + class names

In com. Aopeila. Meetu. Boot. API. The Main as an example, we can be specified as follows respectively different levels of the logger level of output:


logging:

level:

com: debug

com.aopeila: info

com.aopeila.meetu: warn

com.aopeila.meetu.boot.api.Main: error

Copy the code

The logger calculates the output level that the log should use based on the longest prefix match, or if no match is found, the root logger’s output level is used.

The log group

We can group related loggers into a group and configure them in groups.

For example, you can combine all tomcat-related loggers into one group:


logging:

group:

tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"

Copy the code

We put the following loggers:

org.apache.catalina

org.apache.coyote

org.apache.tomcat

Merge into a group Tomcat.

You can then use grouped Tomcat to configure log properties, such as setting the log level uniformly:


logging:

level:

tomcat: "trace"

Copy the code

Set the grouped Tomcat log level to Trace, that is, set the log level to Trace for the three loggers mentioned earlier.

summary

By default, SpringBoot integrates the log function provided by Logback, which can meet most of our daily requirements and is relatively simple to configure. Therefore, you are recommended to use it in actual projects.