json - 通过tail格式化和 pretty-print 日志

标签 json logging grep jq tail

我有这个日志文件,我经常检查它,由于它的格式,打印出来后更容易阅读。我想这样做。

登录文件,如:

2019-07-04T09:53:04-07:00   some.package.placeholder.stderr {"log": "The content", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "I'm actually", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "Interested on", "foo": "bar", "baz": "blah"}

我想做类似的事情
tail -f myLogFile | grep [...?...] | jq '.log'

所以当尾随我得到:
The content
I'm actually
Interested on

甚至:
2019-07-04T09:53:04-07:00   The content
2019-07-04T10:15:37-07:00   I'm actually
2019-07-04T10:15:37-07:00   Interested on

最佳答案

使用 GNU grep -o :

$ tail file | grep -o '{[^}]*}' | jq -r '.log'
The content
I'm actually
Interested on

使用任何 awk:
$ tail file | awk 'sub(/.*{/,"{")' | jq -r '.log'
The content
I'm actually
Interested on

$ tail file | awk '{d=$1} sub(/.*{/,""){$0="{\"date\": \""d"\", " $0} 1' | jq -r '.date + " " + .log'
2019-07-04T09:53:04-07:00 The content
2019-07-04T10:15:37-07:00 I'm actually
2019-07-04T10:15:37-07:00 Interested on

最后一个的工作原理是将输入中的日期字段合并到 json 中,这样 jq 就可以选择并使用日志字段打印它。

关于json - 通过tail格式化和 pretty-print 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56892361/

相关文章:

Java:JSON -> Protobuf & 反向转换

linux - 在bash中解析conf文件

php - TypeError : data. isbn 未定义

java - 如何使用 Java 处理 JSON 响应中的可选键

python - 记录器配置以记录到文件并打印到标准输出

android - 如何捕获/记录 Android 应用程序进行的 api 调用(函数调用)?

python - Popen 管道 2 cmd 挂起并且没有给出预期的结果

regex - 如何将正则表达式转换为 grep 格式

json - 如何通过 Elm 端口传递联合类型?

javascript - 覆盖console.log(或一般的任何函数): Is there a proper way to do this?