spring boot 日志配置详解

时间:2021-09-08 06:54:34

最近在学习spring boot框架的路上,今日看了一下spring boot日志配置,顺便留个笔记记录一下。

1.新建logback.xml文件

spring boot 日志配置详解

内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
<configuration scan="true" scanPeriod="10 seconds">
 <include resource="org/springframework/boot/logging/logback/base.xml" />
 
 <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <File>${LOG_PATH}/info.log</File>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
   <fileNamePattern>${LOG_PATH}/info-%d{yyyyMMdd}.log.%i</fileNamePattern>
   <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <maxFileSize>500MB</maxFileSize>
   </timeBasedFileNamingAndTriggeringPolicy>
   <maxHistory>2</maxHistory>
  </rollingPolicy>
  <layout class="ch.qos.logback.classic.PatternLayout">
   <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
   </Pattern>
  </layout>
 </appender>
 
 <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
   <level>ERROR</level>
  </filter>
  <File>${LOG_PATH}/error.log</File>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
   <fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i
   </fileNamePattern>
   <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <maxFileSize>500MB</maxFileSize>
   </timeBasedFileNamingAndTriggeringPolicy>
   <maxHistory>2</maxHistory>
  </rollingPolicy>
  <layout class="ch.qos.logback.classic.PatternLayout">
   <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
 
   </Pattern>
  </layout>
 </appender>
 
 <!-- hibernate日志输入 -->
 <!-- <logger name="org.hibernate.type.descriptor.sql.BasicBinder"
  level="TRACE" />
 <logger name="org.hibernate.type.descriptor.sql.BasicExtractor"
  level="TRACE" />
 <logger name="org.hibernate.SQL" level="INFO" />
 <logger name="org.hibernate.engine.QueryParameters" level="INFO" />
 <logger name="org.hibernate.engine.query.HQLQueryPlan" level="INFO" /> -->
 
 <root level="INFO">
  <appender-ref ref="INFO_FILE" />
  <appender-ref ref="ERROR_FILE" />
 </root>
 
</configuration>

2.然后在application.properties文件中假如如下代码:

?
1
2
3
#log
logging.config=classpath:logback.xml
logging.path=/workspace/rock/jpa-test/log

3.这时就可以使用log对象了

?
1
2
3
private final Logger log = Logger.getLogger(this.getClass());
 
log.info("你好");

注:由于我的项目在d盘,所以第二步设置的文件路径为:d:/workspace/rock/jpa-test/log

运行程序后,进入路径会有三个文件

spring boot 日志配置详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/zb38/p/5411701.html