java應用中,日志一般分為以下5個級別:
- error 錯誤信息
- warn 警告信息
- info 一般信息
- debug 調試信息
- trace 跟蹤信息
spring boot使用apache的commons logging作為內部的日志框架,其僅僅是一個日志接口,在實際應用中需要為該接口來指定相應的日志實現。
springbt默認的日志實現是java util logging,是jdk自帶的日志包,此外springbt當然也支持log4j、logback這類很流行的日志實現。
統一將上面這些 日志實現 統稱為 日志框架
下面我們來實踐一下!
使用spring boot logging插件
首先application.properties文件中加配置:
1
|
logging.level.root=info |
控制器部分代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.hansonwang99.controller; import com.hansonwang99.k8sresctrlapplication; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping ( "/testlogging" ) public class loggingtestcontroller { private static logger logger = loggerfactory.getlogger(k8sresctrlapplication. class ); @getmapping ( "/hello" ) public string hello() { logger.info( "test logging..." ); return "hello" ; } } |
運行結果
由于將日志等級設置為info,因此包含info及以上級別的日志信息都會打印出來
這里可以看出,很多大部分的info日志均來自于springbt框架本身,如果我們想屏蔽它們,可以將日志級別統一先全部設置為error,這樣框架自身的info信息不會被打印。然后再將應用中特定的包設置為debug級別的日志,這樣就可以只看到所關心的包中的debug及以上級別的日志了。
控制特定包的日志級別
application.yml中改配置
1
2
3
4
|
logging: level: root: error com.hansonwang99.controller: debug |
很明顯,將root日志級別設置為error,然后再將 com.hansonwang99.controller
包的日志級別設為debug,此即:即先禁止所有再允許個別的 設置方法
控制器代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.hansonwang99.controller; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping ( "/testlogging" ) public class loggingtestcontroller { private logger logger = loggerfactory.getlogger( this .getclass()); @getmapping ( "/hello" ) public string hello() { logger.info( "test logging..." ); return "hello" ; } } |
運行結果
可見框架自身的info級別日志全部藏匿,而指定包中的日志按級別順利地打印出來
將日志輸出到某個文件中
1
2
3
4
5
|
logging: level: root: error com.hansonwang99.controller: debug file: ${user.home}/logs/hello.log |
運行結果
使用spring boot logging,我們發現雖然日志已輸出到文件中,但控制臺中依然會打印一份,發現用 org.slf4j.logger
是無法解決這個問題的
集成log4j日志框架
pom.xml中添加依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-logging</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-log4j2</artifactid> </dependency> |
在resources目錄下添加 log4j2.xml
文件,內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration> <appenders> <file name= "file" filename= "${sys:user.home}/logs/hello2.log" > <patternlayout pattern= "%d{hh:mm:ss,sss} %p %c (%l) - %m%n" /> </file> </appenders> <loggers> <root level= "error" > <appender-ref ref= "file" /> </root> <logger name= "com.hansonwang99.controller" level= "debug" /> </loggers> </configuration> |
其他代碼都保持不變
運行程序發現控制臺沒有日志輸出,而hello2.log文件中有內容,這符合我們的預期:
而且日志格式和 pattern="%d{hh:mm:ss,sss} %p %c (%l) - %m%n"
格式中定義的相匹配
log4j更進一步實踐
pom.xml配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-logging</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-log4j2</artifactid> </dependency> |
log4j2.xml配置
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration status= "warn" > <properties> <property name= "app_name" >springboot-web</property> <property name= "log_path" >logs/${app_name}</property> </properties> <appenders> <console name= "console" target= "system_out" > <patternlayout pattern= "[%d][%t][%p][%l] %m%n" /> </console> <rollingfile name= "rollingfileinfo" filename= "${log_path}/info.log" filepattern= "${log_path}/$${date:yyyy-mm}/info-%d{yyyy-mm-dd}-%i.log.gz" > <filters> <thresholdfilter level= "info" /> <thresholdfilter level= "warn" onmatch= "deny" onmismatch= "neutral" /> </filters> <patternlayout pattern= "[%d][%t][%p][%c:%l] %m%n" /> <policies> <!-- 歸檔每天的文件 --> <timebasedtriggeringpolicy interval= "1" modulate= "true" /> <!-- 限制單個文件大小 --> <sizebasedtriggeringpolicy size= "2 mb" /> </policies> <!-- 限制每天文件個數 --> <defaultrolloverstrategy compressionlevel= "0" max= "10" /> </rollingfile> <rollingfile name= "rollingfilewarn" filename= "${log_path}/warn.log" filepattern= "${log_path}/$${date:yyyy-mm}/warn-%d{yyyy-mm-dd}-%i.log.gz" > <filters> <thresholdfilter level= "warn" /> <thresholdfilter level= "error" onmatch= "deny" onmismatch= "neutral" /> </filters> <patternlayout pattern= "[%d][%t][%p][%c:%l] %m%n" /> <policies> <!-- 歸檔每天的文件 --> <timebasedtriggeringpolicy interval= "1" modulate= "true" /> <!-- 限制單個文件大小 --> <sizebasedtriggeringpolicy size= "2 mb" /> </policies> <!-- 限制每天文件個數 --> <defaultrolloverstrategy compressionlevel= "0" max= "10" /> </rollingfile> <rollingfile name= "rollingfileerror" filename= "${log_path}/error.log" filepattern= "${log_path}/$${date:yyyy-mm}/error-%d{yyyy-mm-dd}-%i.log.gz" > <thresholdfilter level= "error" /> <patternlayout pattern= "[%d][%t][%p][%c:%l] %m%n" /> <policies> <!-- 歸檔每天的文件 --> <timebasedtriggeringpolicy interval= "1" modulate= "true" /> <!-- 限制單個文件大小 --> <sizebasedtriggeringpolicy size= "2 mb" /> </policies> <!-- 限制每天文件個數 --> <defaultrolloverstrategy compressionlevel= "0" max= "10" /> </rollingfile> </appenders> <loggers> <root level= "info" > <appender-ref ref= "console" /> <appender-ref ref= "rollingfileinfo" /> <appender-ref ref= "rollingfilewarn" /> <appender-ref ref= "rollingfileerror" /> </root> </loggers> </configuration> |
控制器代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.hansonwang99.controller; import org.apache.logging.log4j.logmanager; import org.apache.logging.log4j.logger; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping ( "/testlogging" ) public class loggingtestcontroller { private final logger logger = logmanager.getlogger( this .getclass()); @getmapping ( "/hello" ) public string hello() { for ( int i= 0 ;i<10_0000;i++){ logger.info( "info execute index method" ); logger.warn( "warn execute index method" ); logger.error( "error execute index method" ); } return "my first springboot application" ; } } |
運行結果
日志會根據不同的級別存儲在不同的文件,當日志文件大小超過2m以后會分多個文件壓縮存儲,生產環境的日志文件大小建議調整為20-50mb。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://juejin.im/post/5abc3bc0f265da237b2227e7