[Note] [https://www.tutorialspoint.com/spring_boot/spring_boot_logging.htm](https://www.tutorialspoint.com/spring_boot/spring_ boot_logging.htm)
Spring Boot uses Apache Commons Logging for internal logging. The Spring Boot default configuration provides support for Java Util Logging, Log4j2, and Logback. In this way, we can configure console or file logging.
If you use a Spring Boot launcher, Logback provides good logging support. Logback also has good support for Common Logging, Util Logging, Log4J, and SLF4J.

The Log format

The default Spring Boot log format is as follows:
! [](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e4fd11c752e74055817a8afa67af2b1c~tplv-k3u1fbpfcp-zoom-1.image)
The following information is provided:
  • Date and Time Indicates the Date and Time of the log
  • Log level Displays INFO, ERROR, or WARN
  • Process ID
  • Use — as the separator
  • Thread name is in square brackets []
  • Logger Name Displays the Name of the source class
  • Log message

Console Log Output

By default, log messages are printed to the console window. In absence, the “INFO”, “ERROR”, and “WARN” log messages are printed in the log file.
To allow debug level logging, start the application from the command line with the debug flag as shown below:
Java – jar demo. Jar — — the debug
You can also add debug mode to application.properties, as follows:
debug = true

File Log Output

By default, all logs are not printed to the console window instead of in a file. If you want to print logs to a file, you need to set the logging.file or logging.path property in the application.properties file.
You can set the log file path as follows, noting that the log file name is spring.log.
logging.path = /var/tmp/
You can also set your own log file name, as follows:
logging.file = /var/tmp/mylog.log
Note: The file will be dumped automatically when it reaches 10 MB.

The level of logging

Spring Boot supports all log levels, such as “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”. You can define the root log in the application.properties file, as follows:
logging.level.root = WARN
Note: Logback does not support logs of the “FATAL” level, which are mapped to logs of the “ERROR” level.

Configuration Logback

Logback supports XML-based configuration to handle Spring Boot log configuration. The log configuration details are in the logback.xml configuration file. The logback. XML file should be in the classpath path.
You can configure root-level logging in the logback.xml file as follows:
<? XML version = "1.0" encoding = "UTF-8"? ><configuration> <root level = "INFO"> </root></configuration>Copy the code

The console appender can be configured in the logback.xml file as follows:
<? 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>Copy the code

You can configure the logback.xml appender file, as shown below. Note that the log file path needs to be specified in 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>Copy the code

You can define logging patterns in the logback.xml file, or you can define a set of logging patterns in the console or in a file as follows:
<pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
Copy the code

The complete logback. XML file is as follows and needs to be placed 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>Copy the code

The following code shows how to add slf4J Logger to the 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; @SpringBootApplicationpublic 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); }}Copy the code

The console window output looks like this:
! [](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2041fb8a8dc144938718f7b3fe6ba8b5~tplv-k3u1fbpfcp-zoom-1.image)
The log file output is as follows:
! [](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4bf656db2c3b40b8a0c160ebabc42a0b~tplv-k3u1fbpfcp-zoom-1.image)