Spring Boot Actuator 提供了几个端点来监控一个应用程序:
/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}
这适合机器使用,但很难被人类阅读。
我想格式化(即 pretty-print )Spring Boot Actuator 端点的 JSON 输出,使其更易于阅读由操作人员。
类似:
{
"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
但此设置不影响执行器输出。
是否有配置来启用 pretty-print Spring Boot Actuator JSON输出?
更新:
official sample为我工作。
关注@DaveSyer 的评论很重要:要设置的属性是
http.mappers.jsonPrettyPrint=true
调查仍在进行中。
与此同时,我使用 json pretty-print 命令行 作为解决方法:
安装 jsonpp(例如用于 OS X):
brew install jsonpp
然后将 curl 输出通过 jsonpp 管道传输,它会即时格式化 json 文件:
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,
...
}
最佳答案
根据 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper ,在 Spring Boot(至少 1.2.2)中使用 Jackson 启用 pretty-print 的官方方法是设置以下属性:
# Pretty-print JSON responses
spring.jackson.serialization.indent_output=true
关于json - Spring Boot Actuator 端点的 pretty-print JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503790/