首先logbok-spring.xml里面的内容
<?xml version="1.0" encoding="UTF-8"?>
<configuration><!-- 开发、测试环境 --><springProfile name="dev,test"><include resource="org/springframework/boot/logging/logback/base.xml" /><root level="INFO"><appender-ref ref="CONSOLE" /><appender-ref ref="FILE" /></root><logger name="org.springframework.web" level="DEBUG"/><logger name="org.springboot.sample" level="DEBUG" /><logger name="com.book_store.bookstore" level="DEBUG" /></springProfile><!-- 生产环境 --><springProfile name="pro"><logger name="org.springframework.web" level="INFO"/><logger name="org.springboot.sample" level="INFO" /><logger name="com.book_store.bookstore" level="ERROR" /></springProfile>
</configuration>
然后再application.yml中配置
spring:profiles:#开发环境日志配置active: dev
这样就是先springboot环境多配置了
中途知识补给
<include resource="org/springframework/boot/logging/logback/base.xml" />
这个东西是springboot中自带的基础xml文件,本地jar看了看没有,去github开源的源码中找到了这个文件,官方地址:
spring-boot/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/base.xml at main · spring-projects/spring-boot · GitHubSpring Boot. Contribute to spring-projects/spring-boot development by creating an account on GitHub.https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/base.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?><!--
Base logback configuration provided for compatibility with Spring Boot 1.1
--><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>
这里面的内容都是可以复写的,其中defaults也在上面的连接中
defaults.xml
<?xml version="1.0" encoding="UTF-8"?><!--
Default logback configuration provided for import
--><included><conversionRule conversionWord="applicationName" converterClass="org.springframework.boot.logging.logback.ApplicationNameConverter" /><conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /><conversionRule conversionWord="correlationId" converterClass="org.springframework.boot.logging.logback.CorrelationIdConverter" /><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'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr(%applicationName[%15.15t]){faint} %clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/><property name="CONSOLE_LOG_THRESHOLD" value="${CONSOLE_LOG_THRESHOLD:-TRACE}"/><property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- %applicationName[%t] ${LOG_CORRELATION_PATTERN:-}%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><property name="FILE_LOG_CHARSET" value="${FILE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/><property name="FILE_LOG_THRESHOLD" value="${FILE_LOG_THRESHOLD:-TRACE}"/><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"/><logger name="org.springframework.boot.actuate.endpoint.jmx" level="WARN"/>
</included>
root中的level就是输出级别
<root level="INFO"><appender-ref ref="CONSOLE" /><appender-ref ref="FILE" /></root>