How would a human read a json winston log file?

此生再无相见时 提交于 2019-12-02 17:29:26

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'); 

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

jq . file.log 
Brian Agnew

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

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

You should try winston-logs-display .

Demo Output:

Also Log.io is good option for this. it supports winston log.

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) 

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

./thing | ndjson 

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!