如何根据日期过滤bunyan日志?

时间:2016-06-22 20:15:24

标签: node.js bunyan

有一个bunyan日志,这是一个示例条目

[2016-05-31T15:38:47.889Z] FATAL: jklajsd-utilities/23399 on aaa.bbb.ccc.com: 
    0: {
      "code": "EADDRINUSE",
      "errno": "EADDRINUSE",
      "syscall": "listen",
      "address": "0.0.0.0",
      "port": 5566
    }

好的我想在那之后找到所有条目:

bunyan /opt/aaa/.pm2/logs/cccc-out-15.log -c 'this.date >= new Date("2016-05-31T15:38:47.889Z")' 

没有结果。也试过this.time。 bunyan文档说time是正确的领域,但它不起作用。 new Date("2016-05-31T15:38:47.889Z")'有效:

> new Date("2016-05-31T15:58:50.475Z")
Tue May 31 2016 08:58:50 GMT-0700 (PDT)

日志过滤肯定有效:

$ bunyan /opt/aaa/.pm2/logs/cccc-out-15.log -c 'this.level === DEBUG ' |wc -l 
102455

$ bunyan /opt/aaa/.pm2/logs/cccc-out-15.log -c 'this.level === FATAL '|wc -l 
1679

将年份改为2015年没有做任何事情。

那么我在日期过滤方面做错了什么?

2 个答案:

答案 0 :(得分:2)

JSON中的时间存储为字符串。所以你需要在比较之前进行转换。

bunyan /opt/aaa/.pm2/logs/cccc-out-15.log -c 'new Date(this.time) >= new Date("2016-05-31T15:38:47.889Z")'

答案 1 :(得分:1)

答案似乎是在调用bunyan之前过滤:

cat /opt/xyz/.pm2/logs/whiskey-tango-out-15.log | grep "2016-10-11" | bunyan -o short

产生:

[2016-10-11T22:33:08.836Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 3
[2016-10-11T22:33:08.837Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 2
[2016-10-11T22:33:08.837Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 1
[2016-10-11T22:33:08.838Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 3
[2016-10-11T22:33:08.838Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 2
[2016-10-11T22:33:08.838Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 2
[2016-10-11T22:33:08.839Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 6
[2016-10-11T22:33:08.839Z]  WARN: whiskey-tango/8828 on [redacted]: Post failed, retrying, count: 3

适合我。看起来它应该可以使用bunyan命令行。