How would a human read a json winston log file?

2020-05-17 02:59发布

It seems nice for API's, scripts and what not. But reading a winston json stack trace is very hard with a text editor. E.g.

{"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\\nodejs\\node.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\\node_modules\\request\\request.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\\node_modules\\request\\request.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\\node_modules\\request\\request.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\\node_modules\\request\\request.js:122:22)","    at Request.EventEmitter.emit (events.js:98:17)","    at Request.<anonymous> (C:\\data\\mytool\\node_modules\\request\\request.js:888:14)","    at Request.EventEmitter.emit (events.js:117:20)","    at IncomingMessage.<anonymous> (C:\\data\\mytool\\node_modules\\request\\request.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"}

8条回答
Deceive 欺骗
2楼-- · 2020-05-17 03:39

Pass it through jq, which is like sed for JSON. E.g.:

jq . file.log
查看更多
甜甜的少女心
3楼-- · 2020-05-17 03:46

It's slow but your shell can do it, get formatted, colourized JSON.

./thing | ndjson

asciicast

How?

You run some JSON formatting command on each line, bash or zsh syntax is:

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

For fish the syntax is

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

To make it extra fancy, just pip install pygments.

Define a handy alias pp, so you run cat file.json | pp.

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

And then define ndjson

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

Now you can type the following to get formatted, colourized JSON.

./thing | ndjson

(use funced and funcsave to define alias in fish)

查看更多
迷人小祖宗
4楼-- · 2020-05-17 03:53

Seems node's bunyan has features that let you filter and view json logs in a human readable way with a CLI.

$ 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

查看更多
太酷不给撩
5楼-- · 2020-05-17 03:54

If you use Keen.IO - their CLI tool can upload the line-deliminated JSON, then you can use their 'Explorer' to filter/view log events.

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

查看更多
孤傲高冷的网名
6楼-- · 2020-05-17 03:55

Why not just run it through a JSON formatter on the command line ?

e.g. (example from the link above)

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

An alternative may be to look at building a shell script around the above tool (or perhaps) jq to perform some custom stack trace parsing

查看更多
Rolldiameter
7楼-- · 2020-05-17 03:58

Simply set the file transport "json" property to false, and you'll get a human readable log. Same as you see in the console.

    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');
查看更多
登录 后发表回答