Spring Boot 简明教程

Spring Boot - Logging

Spring Boot 使用 Apache Commons 记录所有内部记录。Spring Boot 的默认配置为使用 Java Util 记录、Log4j2 和 Logback 提供支持。使用这些,我们可以配置控制台记录以及文件记录。

Spring Boot uses Apache Commons logging for all internal logging. Spring Boot’s default configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. Using these, we can configure the console logging as well as file logging.

如果您使用的是 Spring Boot Starter,则 Logback 将很好地支持记录。此外,Logback 同样为通用记录、Util 记录、Log4J 和 SLF4J 提供良好的支持。

If you are using Spring Boot Starters, Logback will provide a good support for logging. Besides, Logback also provides a use of good support for Common Logging, Util Logging, Log4J, and SLF4J.

Log Format

Spring Boot 的 Log 默认格式如下面给出的屏幕截图中所示。

The default Spring Boot Log format is shown in the screenshot given below.

spring boot log format

它向您提供以下信息 −

which gives you the following information −

  1. Date and Time that gives the date and time of the log

  2. Log level shows INFO, ERROR or WARN

  3. Process ID

  4. The --- which is a separator

  5. Thread name is enclosed within the square brackets []

  6. Logger Name that shows the Source class name

  7. The Log message

Console Log Output

默认日志消息将打印到控制台窗口。默认情况下,“INFO”、“ERROR”和“WARN”日志消息将打印到日志文件中。

The default log messages will print to the console window. By default, “INFO”, “ERROR” and “WARN” log messages will print in the log file.

如果您必须启用调试级别日志,请使用以下所示的命令在启动应用程序时添加调试标志 -

If you have to enable the debug level log, add the debug flag on starting your application using the command shown below −

java –jar demo.jar --debug

您还可以将调试模式添加到 application.properties 文件,如下所示 -

You can also add the debug mode to your application.properties file as shown here −

debug = true

File Log Output

默认情况下,所有日志都将打印在控制台窗口中,而不在文件中。如果您想在文件中打印日志,则需要在 application.properties 文件中设置属性 logging.filelogging.path

By default, all logs will print on the console window and not in the files. If you want to print the logs in a file, you need to set the property logging.file or logging.path in the application.properties file.

您可以使用以下所示的属性指定日志文件路径。请注意,日志文件名是 spring.log。

You can specify the log file path using the property shown below. Note that the log file name is spring.log.

logging.path = /var/tmp/

您可以使用以下所示的属性指定自己的日志文件名 -

You can specify the own log file name using the property shown below −

logging.file = /var/tmp/mylog.log

Note - 文件在达到 10 MB 大小时将自动轮换。

Note − files will rotate automatically after reaching the size 10 MB.

Log Levels

Spring Boot 支持所有日志记录级别,例如“TRACE”、“DEBUG”、“INFO”、“WARN”、“ERROR”、“FATAL”、“OFF”。您可以按如下所示在 application.properties 文件中定义 Root 记录器 -

Spring Boot supports all logger levels such as “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”. You can define Root logger in the application.properties file as shown below −

logging.level.root = WARN

Note - Logback 不支持“FATAL”级别日志。它映射到“ERROR”级别日志。

Note − Logback does not support “FATAL” level log. It is mapped to the “ERROR” level log.

Configure Logback

Logback 支持基于 XML 的配置来处理 Spring Boot Log 配置。日志配置详细信息配置在 logback.xml 文件中。logback.xml 文件应放置在类路径下。

Logback supports XML based configuration to handle Spring Boot Log configurations. Logging configuration details are configured in logback.xml file. The logback.xml file should be placed under the classpath.

您可以使用以下给出的代码在 Logback.xml 文件中配置 ROOT 级别日志 -

You can configure the ROOT level log in Logback.xml file using the code given below −

<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
   <root level = "INFO">
   </root>
</configuration>

您可以使用下面给出的代码在 Logback.xml 文件中配置控制台追加程序。

You can configure the console appender in Logback.xml file given below.

<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
   <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender"></appender>
   <root level = "INFO">
      <appender-ref ref = "STDOUT"/>
   </root>
</configuration>

您可以使用下面给出的代码在 Logback.xml 文件中配置文件追加程序。请注意,您需要在文件追加程序内部指定日志文件路径。

You can configure the file appender in Logback.xml file using the code given below. Note that you need to specify the Log file path insider the file appender.

<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
   <appender name = "FILE" class = "ch.qos.logback.core.FileAppender">
      <File>/var/tmp/mylog.log</File>
   </appender>
   <root level = "INFO">
      <appender-ref ref = "FILE"/>
   </root>
</configuration>

您可以使用以下给出的代码在 logback.xml 文件中定义日志模式。您还可以使用以下给出的代码在控制台或文件日志追加程序中定义一组受支持的日志模式 -

You can define the Log pattern in logback.xml file using the code given below. You can also define the set of supported log patterns inside the console or file log appender using the code given below −

<pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>

完整的 logback.xml 文件代码如下。您必须将其放在类路径中。

The code for complete logback.xml file is given below. You have to place this in the class path.

<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
   <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
      </encoder>
   </appender>

   <appender name = "FILE" class = "ch.qos.logback.core.FileAppender">
      <File>/var/tmp/mylog.log</File>
      <encoder>
         <pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
      </encoder>
   </appender>

   <root level = "INFO">
      <appender-ref ref = "FILE"/>
      <appender-ref ref = "STDOUT"/>
   </root>
</configuration>

下面给出的代码展示如何将 slf4j 记录器添加到 Spring Boot 主类文件中。

The code given below shows how to add the slf4j logger in Spring Boot main class file.

package com.tutorialspoint.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
   private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

   public static void main(String[] args) {
      logger.info("this is a info message");
      logger.warn("this is a warn message");
      logger.error("this is a error message");
      SpringApplication.run(DemoApplication.class, args);
   }
}

您可以在控制台窗口中看到的输出显示在这里 -

The output that you can see in the console window is shown here −

logger console window

您可以在日志文件中看到的输出显示在这里 -

The output that you can see in the log file is shown here −

log output