springboot 日志 logback输出
1.首先在 application,yaml中添加
logging:
config: classpath:logback-spring.xml
2.之后在resources中添加 logback-spring.xml。因为最后被java编译后的路径是 上述配置的。见图:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <!--这个名字将反应到每一条日志的开头--> 4 <contextName>logback</contextName> 5 <!--设置变量,下面有用到。含义是日志保存路径--> 6 <property name="log.path" value="./log/"/> 7 8 <!--输出到控制台--> 9 <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> 10 <!-- 级别过滤 --> 11 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 12 <level>INFO</level> 13 <onMatch>ACCEPT</onMatch> 14 <onMismatch>DENY</onMismatch> 15 </filter> 16 <!--日志输出格式--> 17 <encoder> 18 <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> 19 </encoder> 20 </appender> 21 22 <!--输出到文件--> 23 <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> 24 <!--日志名,用到了上面配置的路径--> 25 <file>${log.path}/application.log</file> 26 <!--根据yyyy-MM-dd所以是每天新建文件,也可以设置其它格式,如每月或每分--> 27 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 28 <fileNamePattern>${log.path}/app.%d{yyyy-MM-dd}.log.zip</fileNamePattern> 29 <maxHistory>30</maxHistory> 30 <totalSizeCap>1GB</totalSizeCap> 31 </rollingPolicy> 32 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 33 <MaxFileSize>100MB</MaxFileSize> 34 </triggeringPolicy> 35 <encoder> 36 <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> 37 </encoder> 38 39 </appender> 40 41 <!--设置日志隔离级别--> 42 <root level="info"> 43 <appender-ref ref="console"/> 44 <appender-ref ref="file"/> 45 </root> 46 47 </configuration>
之后日志便会每天进行压缩。如: