Spring Boot Actuator端点的漂亮的JSON输出
Spring Boot Actuator 提供了几个端点,用于监视一个应用:
Spring Boot Actuator provides several endpoints to monitor an application as:
/metrics
/beans
/health
...
使用以下方法检查端点
curl http://localhost:8080/metrics
导致:
{"counter.status.200.env":1,"counter.status.200.health":1,"counter.status.200.info":2,"counter.status.200.metrics":2,"gauge.response.env":5.0,"gauge.response.health":22.0,"gauge.response.info":1.0,"gauge.response.metrics":1.0,"mem":1030144,"mem.free":56118,"processors":8,"uptime":5108095,"instance.uptime":5102906,"heap.committed":1030144,"heap.init":262144,"heap.used":974031,"heap":3728384,"threads.peak":81,"threads.daemon":21,"threads":77,"classes":8854,"classes.loaded":8860,"classes.unloaded":6,"gc.ps_scavenge.count":119,"gc.ps_scavenge.time":7223,"gc.ps_marksweep.count":12,"gc.ps_marksweep.time":17573}
这对机器消耗来说是很好的,但人却很难阅读.
This is fine for machine consumption but hard to read by humans.
我想对 Spring Boot Actuator 端点的 JSON 输出进行格式化(即打印精美),以使其更易于阅读由操作人员负责.
I'd like to format (i.e. pretty print) the JSON output of the Spring Boot Actuator endpoints to make them easier to read by operations personel.
类似的东西:
{
"counter.status.200.env":1,
"counter.status.200.health":1,
"counter.status.200.info":2,
"counter.status.200.metrics":2,
"gauge.response.env":5.0,
"gauge.response.health":22.0,
"gauge.response.info":1.0,
...
}
我尝试设置
http.mappers.json-pretty-print=true
但是此设置不会影响执行器的输出.
but this setting didn't affect the Actuator output.
是否有配置以启用 Spring Boot Actuator JSON 输出的漂亮打印?
Is there a configuration to enable pretty print of the Spring Boot Actuator JSON output?
更新:
官方示例为我工作.
请务必注意@DaveSyer的评论:要设置的属性是
It's important to follow the comments from @DaveSyer: the property to set is
http.mappers.jsonPrettyPrint=true
调查仍在进行中.
同时,我使用json漂亮的打印命令行作为解决方法:
In the meantime I use the the json pretty print command line as workaround:
安装jsonpp(例如,对于OS X):
Install jsonpp (e.g. for OS X):
brew install jsonpp
然后通过管道输出curl输出槽jsonpp,以动态格式化json文件:
Then pipe the curl output trough jsonpp which formats the json file on the fly:
curl http://localhost:8080/metrics | jsonpp
结果:
{
"counter.status.200.env": 1,
"counter.status.200.health": 1,
"counter.status.200.info": 2,
"counter.status.200.metrics": 2,
...
}
As per http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper, the official way to enable pretty print with Jackson in Spring Boot (1.2.2 at least) is to set the following property:
# Pretty-print JSON responses
spring.jackson.serialization.indent_output=true