24.SpringBoot学习笔记--SpringBoot默认配置

日志级别

Spring Boot 默认配置好了日志,日志的级别从低到高为 trace < debug < info < warn < error

在测试类中编写以下代码:

package demo.yangxu.springboot;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot03LoggingApplicationTests {

    //记录器
    Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    void contextLoads() {
        logger.trace("这是trace日志...");
        logger.debug("这是debug日志...");
        logger.info("这是info日志...");
        logger.warn("这是warn日志...");
        logger.error("这是error日志...");
    }
}

在控制台中输出的结果为:

这是info日志...
这是warn日志...
这是error日志...

由此可知,Spring Boot 默认配置的是 info 级别,没有指定级别就用 Spring Boot 默认规定的级别:root 级别。可以调整输出的日志级别,会输出大于等于设置级别的日志。

通过查看 spring-boot-2.3.0.RELEASE.jar!\org\springframework\boot\logging\logback\base.xml 文件

<included>
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
	<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
	<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
	<root level="INFO">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	root>
included>

也可得知 Spring Boot 默认配置的是 info 级别。

配置日志

在 application.properties 中可以对日志进行相关的配置。

#调整指定包的日志级别
logging.level.demo.yangxu=trace
#在当前项目目录下生成springboot.log
logging.file.name=springboot.log
#指定完整路径和名字
logging.file.name=D:/springboot.log
#在当前项目目录下生成spring.log
logging.file.path=./
#在当前项目所在磁盘的 /spring/log 目录下生成spring.log
logging.file.path=/spring/log
#配置控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
#配置指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

logging.file.name 和 logging.file.path 都指定的话,只有 logging.file.name 起作用。一般是指定 logging.file.path。

logging.file.name 和 logging.file.path 对比:

logging.file.name logging.file.path Example Description
(none) (none) Console only logging.
Specific file (none) my.log Writes to the specified log file. Names can be an exact location or relative to the current directory.
(none) Specific directory /var/log Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

日志输出格式:

日志输出格式:
    %d表示日期时间,
    %thread表示线程名,
    %-5level:级别从左显示5个字符宽度
    %logger{50} 表示logger名字最长50个字符,否则按照句点分割。 
    %msg:日志消息,
    %n是换行符
例如
    %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

通过查看 spring-boot-2.3.0.RELEASE.jar!\org\springframework\boot\logging\logback\defaults.xml





<included>
	<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
	<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
	<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
	<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
	<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

	<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
	<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
	<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
	<logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
	<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
	<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
	<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
included>

spring-boot-2.3.0.RELEASE.jar!\org\springframework\boot\logging\logback\file-appender.xml





<included>
	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<encoder>
			<pattern>${FILE_LOG_PATTERN}pattern>
		encoder>
		<file>${LOG_FILE}file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}cleanHistoryOnStart>
			<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}fileNamePattern>
			<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}maxFileSize>
			<maxHistory>${LOG_FILE_MAX_HISTORY:-7}maxHistory>
			<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}totalSizeCap>
		rollingPolicy>
	appender>
included>

spring-boot-2.3.0.RELEASE.jar!\org\springframework\boot\logging\logback\console-appender.xml





<included>
	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
            
			<pattern>${CONSOLE_LOG_PATTERN}pattern>
		encoder>
	appender>
included>

可以看到不同包下规定的日志级别,输出的格式。

在配置文件中自定义的配置,都保存在了 org.springframework.boot.context.logging.LoggingApplicationListener,而这个类与 org.springframework.boot.logging.LoggingSystemProperties 绑定在一起,LoggingApplicationListener 获取的属性会被封装到 LoggingSystemProperties。

你可能感兴趣的:(Spring,Boot)