概述
Java應(yīng)用中,日志一般分為以下5個級別:
- ERROR 錯誤信息
- WARN 警告信息
- INFO 一般信息
- DEBUG 調(diào)試信息
- TRACE 跟蹤信息
Spring Boot使用Apache的Commons Logging作為內(nèi)部的日志框架,其僅僅是一個日志接口,在實際應(yīng)用中需要為該接口來指定相應(yīng)的日志實現(xiàn)。
Spring Boot默認的日志實現(xiàn)是Java Util Logging,是JDK自帶的日志包,此外Spring Boot當然也支持Log4J、Logback這類很流行的日志實現(xiàn)。
統(tǒng)一將上面這些日志實現(xiàn)統(tǒng)稱為日志框架
下面我們來實踐一下!
使用Spring Boot Logging插件
- 首先application.properties文件中加配置:
logging.level.root=INFO
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() {http://logger.info(
"test logging..."
);
return
"hello"
;
}
}
由于將日志等級設(shè)置為INFO,因此包含INFO及以上級別的日志信息都會打印出來
這里可以看出,很多大部分的INFO日志均來自于SpringBt框架本身,如果我們想屏蔽它們,可以將日志級別統(tǒng)一先全部設(shè)置為ERROR,這樣框架自身的INFO信息不會被打印。然后再將應(yīng)用中特定的包設(shè)置為DEBUG級別的日志,這樣就可以只看到所關(guān)心的包中的DEBUG及以上級別的日志了。
application.yml中改配置
logging:level:root: errorcom.hansonwang99.controller: debug
很明顯,將root日志級別設(shè)置為ERROR,然后再將 com.hansonwang99.controller包的日志級別設(shè)為DEBUG,此即:即先禁止所有再允許個別的 設(shè)置方法
packagecom.hansonwang99.controller;
importorg.slf4j.
Logger
;
importorg.slf4j.
LoggerFactory
;
importorg.springframework.web.bind.annotation.
GetMapping
;
importorg.springframework.web.bind.annotation.
RequestMapping
;
importorg.springframework.web.bind.annotation.
RestController
;
@RestController
@RequestMapping
(
"/testlogging"
)
publicclassLoggingTestController{privateLoggerlogger =
LoggerFactory
.getLogger(
this
.getClass());@GetMapping
(
"/hello"
)publicStringhello() {logger.info(
"test logging..."
);return"hello"
;}
}
可見框架自身的INFO級別日志全部藏匿,而指定包中的日志按級別順利地打印出來
logging:level:root: errorcom.hansonwang99.controller: debugfile: ${user.home}/logs/hello.log
使用Spring Boot Logging,我們發(fā)現(xiàn)雖然日志已輸出到文件中,但控制臺中依然會打印一份,發(fā)現(xiàn)用 org.slf4j.Logger是無法解決這個問題的
集成Log4J日志框架
<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文件,內(nèi)容如下:
<?xml version=
"1.0"encoding=
"UTF-8"
?>
<configuration><appenders><Filename
=
"file"fileName
=
"${sys:user.home}/logs/hello2.log"
><PatternLayoutpattern
=
"%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"
/></File></appenders><loggers><rootlevel
=
"ERROR"
><appender-refref
=
"file"
/></root><loggername
=
"com.hansonwang99.controller"level
=
"DEBUG"/></loggers>
</configuration>
運行程序發(fā)現(xiàn)控制臺沒有日志輸出,而hello2.log文件中有內(nèi)容,這符合我們的預(yù)期:
而且日志格式和 pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"格式中定義的相匹配
Log4J更進一步實踐
<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>
<?xml version=
"1.0"encoding=
"UTF-8"
?>
<configurationstatus
=
"warn"
><properties><Propertyname
=
"app_name"
>
springboot-web
</Property><Propertyname
=
"log_path"
>
logs/${app_name}
</Property></properties><appenders><consolename
=
"Console"target
=
"SYSTEM_OUT"
><PatternLayoutpattern
=
"[%d][%t][%p][%l] %m%n"/></console><RollingFilename
=
"RollingFileInfo"fileName
=
"${log_path}/info.log"filePattern
=
"${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
><Filters><ThresholdFilterlevel
=
"INFO"/><ThresholdFilterlevel
=
"WARN"onMatch
=
"
DENY
"onMismatch
=
"
NEUTRAL
"/></Filters><PatternLayoutpattern
=
"[%d][%t][%p][%c:%L] %m%n"/><Policies><!-- 歸檔每天的文件 --><TimeBasedTriggeringPolicyinterval
=
"1"modulate
=
"true"/><!-- 限制單個文件大小 --><SizeBasedTriggeringPolicysize
=
"2 MB"/></Policies><!-- 限制每天文件個數(shù) --><DefaultRolloverStrategycompressionLevel
=
"0"max
=
"10"
/></RollingFile><RollingFilename
=
"RollingFileWarn"fileName
=
"${log_path}/warn.log"filePattern
=
"${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"
><Filters><ThresholdFilterlevel
=
"WARN"/><ThresholdFilterlevel
=
"ERROR"onMatch
=
"
DENY
"onMismatch
=
"
NEUTRAL
"/></Filters><PatternLayoutpattern
=
"[%d][%t][%p][%c:%L] %m%n"/><Policies><!-- 歸檔每天的文件 --><TimeBasedTriggeringPolicyinterval
=
"1"modulate
=
"true"/><!-- 限制單個文件大小 --><SizeBasedTriggeringPolicysize
=
"2 MB"/></Policies><!-- 限制每天文件個數(shù) --><DefaultRolloverStrategycompressionLevel
=
"0"max
=
"10"
/></RollingFile><RollingFilename
=
"RollingFileError"fileName
=
"${log_path}/error.log"filePattern
=
"${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
><ThresholdFilterlevel
=
"ERROR"/><PatternLayoutpattern
=
"[%d][%t][%p][%c:%L] %m%n"/><Policies><!-- 歸檔每天的文件 --><TimeBasedTriggeringPolicyinterval
=
"1"modulate
=
"true"/><!-- 限制單個文件大小 --><SizeBasedTriggeringPolicysize
=
"2 MB"/></Policies><!-- 限制每天文件個數(shù) --><DefaultRolloverStrategycompressionLevel
=
"0"max
=
"10"
/></RollingFile></appenders><loggers><rootlevel
=
"info"
><appender-refref
=
"Console"/><appender-refref
=
"RollingFileInfo"/><appender-refref
=
"RollingFileWarn"/><appender-refref
=
"RollingFileError"/></root></loggers>
</configuration>
packagecom.hansonwang99.controller;
importorg.apache.logging.log4j.
LogManager
;
importorg.apache.logging.log4j.
Logger
;
importorg.springframework.web.bind.annotation.
GetMapping
;
importorg.springframework.web.bind.annotation.
RequestMapping
;
importorg.springframework.web.bind.annotation.
RestController
;
@RestController
@RequestMapping
(
"/testlogging"
)
publicclassLoggingTestController{privatefinalLoggerlogger =
LogManager
.getLogger(
this
.getClass());@GetMapping
(
"/hello"
)publicStringhello() {for
(
inti=
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"
;}
}
日志會根據(jù)不同的級別存儲在不同的文件,當日志文件大小超過2M以后會分多個文件壓縮存儲,生產(chǎn)環(huán)境的日志文件大小建議調(diào)整為20-50MB。
總結(jié)
以上是生活随笔為你收集整理的设置springboot日志级别_Spring Boot 日志框架实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。