添加一个配置文件\src\main\resources\logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?><configuration scan="true" scanPeriod="10 seconds"> <contextName>logbackcontextName> <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> <property name="FILE_PATH" value="C:/tmp/spring-log.%d{yyyy-MM-dd}.%i.log" /> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFOlevel> filter> <encoder> <pattern>${LOG_PATTERN}pattern> encoder> appender> <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${FILE_PATH}fileNamePattern> <maxHistory>30maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>2MBmaxFileSize> timeBasedFileNamingAndTriggeringPolicy> <totalSizeCap>1GBtotalSizeCap> rollingPolicy> <encoder> <pattern>${LOG_PATTERN}pattern> encoder> appender> <logger name="net.sh.rgface.serive" level="ERROR" /> <root level="INFO"> <appender-ref ref="console" /> <appender-ref ref="rollingFile" /> root>configuration>
直接在代码中使用
package com.zz.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@Slf4jpublic class LogBackController { @GetMapping("/logtest1") public String test1(){ log.debug("debug测试日志消息"); log.info("info 测试日志信息"); log.error("info 测试日志信息"); log.warn("warn 测试日志信息"); return "ok"; }}
运行结果