关于node.js:人们如何读取json winston日志文件?

How would a human read a json winston log file?

这对API、脚本和其他应用程序来说似乎很好。但是使用文本编辑器读取WinstonJSON堆栈跟踪非常困难。例如。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{"level":"info","message":"starting","timestamp":"2014-05-14T15:45:44.334Z"}
{"date":"Wed May 14 2014 08:45:45 GMT-0700 (Pacific Daylight Time)","process":{"pid":8804,"uid":null,"gid":null,"cwd":"C:\\data\\mytool","execPath":"C:\\Program Files\
odejs\
ode.exe","version":"v0.10.21","argv":["node","C:\\data\\mytool\\server"],"memoryUsage":{"rss":45199360,"heapTotal":32171264,"heapUsed":15158096}},"os":{"loadavg":[0,0,0],"uptime":70496.6138252},"trace":[{"column":null,"file":null,"function":"Object.parse","line":null,"method":"parse","native":true},{"column":32,"file":"C:\\data\\mytool\\src\\status.js","function":"Request._callback","line":166,"method":"_callback","native":false},{"column":22,"file":"C:\\data\\mytool\
ode_modules\
equest\
equest.js","function":"Request.self.callback","line":122,"method":"self.callback","native":false},{"column":17,"file":"events.js","function":"Request.EventEmitter.emit","line":98,"method":"EventEmitter.emit","native":false},{"column":14,"file":"C:\\data\\mytool\
ode_modules\
equest\
equest.js","function":"","line":888,"method":null,"native":false},{"column":20,"file":"events.js","function":"Request.EventEmitter.emit","line":117,"method":"EventEmitter.emit","native":false},{"column":12,"file":"C:\\data\\mytool\
ode_modules\
equest\
equest.js","function":"","line":839,"method":null,"native":false},{"column":20,"file":"events.js","function":"IncomingMessage.EventEmitter.emit","line":117,"method":"EventEmitter.emit","native":false},{"column":16,"file":"_stream_readable.js","function":null,"line":920,"method":null,"native":false},{"column":13,"file":"node.js","function":"process._tickCallback","line":415,"method":"_tickCallback","native":false}],"stack":["SyntaxError: Unexpected end of input","    at Object.parse (native)","    at Request._callback (C:\\data\\mytool\\src\\status.js:166:32)","    at Request.self.callback (C:\\data\\mytool\
ode_modules\
equest\
equest.js:122:22)","    at Request.EventEmitter.emit (events.js:98:17)","    at Request. (C:\\data\\mytool\
ode_modules\
equest\
equest.js:888:14)","    at Request.EventEmitter.emit (events.js:117:20)","    at IncomingMessage. (C:\\data\\mytool\
ode_modules\
equest\
equest.js:839:12)","    at IncomingMessage.EventEmitter.emit (events.js:117:20)","    at _stream_readable.js:920:16","    at process._tickCallback (node.js:415:13)"],"level":"error","message":"uncaughtException: Unexpected end of input","timestamp":"2014-05-14T15:45:45.228Z"}

只需将文件传输"json"属性设置为false,就可以得到一个可读的日志。和在控制台中看到的一样。

1
2
3
4
5
6
7
8
9
10
11
12
    var winston = require('winston');
    var logger = new winston.Logger({
      transports: [
        new winston.transports.File({
          json: false,
          filename:'log.log'
        }),
        new winston.transports.Console()
      ],
      exitOnError: false
    });
   logger.log('info', 'some msg');


通过jq,就像json的sed一样。例如。:

1
jq . file.log


为什么不在命令行上通过JSON格式化程序运行它呢?

例如(以上链接中的示例)

1
echo '{ element0:"lorem", element1:"ipsum" }' | python -mjson.tool

另一种选择可能是围绕上述工具(或者可能是JQ)构建一个shell脚本,以执行一些定制的堆栈跟踪分析。


你应该试试温斯顿日志显示。

演示输出:

氧化镁

另外,log.io也是很好的选择。它支持Winston日志。


如果您使用keen.io,他们的cli工具可以上传行取消了标记的json,那么您可以使用他们的"explorer"来过滤/查看日志事件。

keen events:add --collection myLogs --file winston-output.json


它很慢,但你的外壳可以做到,格式化,着色JSON。

1
./thing | ndjson

氧化镁

怎么用?

在每一行上运行一些json格式化命令,bashzsh语法为:

1
./thing | while read in ; do echo"$in" | python -m json.tool ; done

对于fish来说,语法是

1
./thing | while read in; echo"$in" | python -mjson.tool; end #fish

为了让它更加花哨,只需pip install pygments

定义一个方便的别名pp,这样就可以运行cat file.json | pp

1
alias pp="python -mjson.tool | pygmentize -l js"

然后定义ndjson

1
alias ndjson='while read in; do echo"$in" | pp; done'

现在,您可以键入以下内容来获得格式化的彩色JSON。

1
./thing | ndjson

(使用funcedfuncsavefish中定义别名)


似乎节点的bunyan具有一些特性,可以使用cli以人类可读的方式过滤和查看JSON日志。

1
2
$ node hi.js | bunyan -l warn
[2013-01-04T19:08:37.182Z]  WARN: myapp/40353 on banana.local: au revoir (lang=fr)

bunyan CLI output