springboot对logback的支持是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以实现功能
直接贴代码:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds"> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-M-d HH:mm:ss} %t %p %m%n</pattern> </encoder> </appender> <appender name="springboot" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- <Encoding>UTF-8</Encoding> --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/logback/springboot_%d{yyyy-M-d}.log </FileNamePattern> <MaxHistory>10</MaxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-M-d HH:mm:ss} %t %p %m%n</pattern> </encoder> </appender> <appender name="smile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/logback/smile_%d{yyyy-M-d}.log </FileNamePattern> <MaxHistory>10</MaxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-M-d HH:mm:ss} %t %p %m%n</pattern> </encoder> </appender> <logger name="org.springframework.boot" level="debug" additivity="false"> <appender-ref ref="springboot" /> </logger> <!-- name包必须能够扫描到所以类,包括启动类 --> <logger name="com.smile" level="debug" additivity="false"> <appender-ref ref="smile" /> </logger> <root level="info"> <appender-ref ref="stdout" /> </root> </configuration>
其中值得注意的是:你配置文件中的包名要能够扫描到你的所有类,包括启动类。
否则,你的日志文件是生成了,但是不会向里面写入内容
启动程序打印日志,日志成功生成: