1.默认情况下直接依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>
2.创建一个名为:logback-spring.xml的文件
3.在yml配置文件中激活
注:优先级logback.xml(默认) > logback-spring.xml,如果发现不生效时,检查下配置是否存在覆盖
# logging配置
logging:level:root: infoconfig: classpath:logback-spring.xml
4.下面为可以进行参考的xml配置
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="10 seconds"><contextName>logback</contextName><springProperty scope="context" name="APP_NAME" source="spring.application.name"/><property name="LOG_PATH" value="/app/logs/${APP_NAME}"/><!-- 彩色日志(IDE下载插件才可以生效) --><!-- 彩色日志依赖的渲染类 --><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{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}}"/><!--输出到控制台 --><appender name="CONSOLE"class="ch.qos.logback.core.ConsoleAppender"><encoder><Pattern>${CONSOLE_LOG_PATTERN}</Pattern><!-- 设置字符集 --><charset>UTF-8</charset></encoder></appender><!--输出到文件 --><!-- 时间滚动输出 所有level 日志 --><appender name="ALL_FILE"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/system_detail.log</file><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset></encoder><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/detail/system_detail-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>30MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天数 --><maxHistory>15</maxHistory></rollingPolicy></appender><!-- 时间滚动输出 level为 ERROR 日志 --><appender name="ERROR_FILE"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/system_error.log</file><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset></encoder><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/error/system_error-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>30MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><maxHistory>15</maxHistory></rollingPolicy><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>ERROR</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!--自定义日志设置--><!--禁用TransLog日志打印--><logger name="TransLog" level="OFF" additivity="false"/><!--禁止ConfigClusterResolver的info级别日志打印--><logger name="com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver" level="WARN" additivity="false"/><root level="info"><appender-ref ref="CONSOLE"/><appender-ref ref="ALL_FILE"/><appender-ref ref="ERROR_FILE"/></root></configuration>