人们如何阅读json winston日志文件?

时间:2014-05-14 16:05:49

标签: json node.js winston

对于API,脚本和其他内容来说,这似乎很好。但是使用文本编辑器读取winston json堆栈跟踪非常困难。 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"}

11 个答案:

答案 0 :(得分:21)

只需将文件传输“json”属性设置为false,即可获得人类可读的日志。与您在控制台中看到的相同。

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

答案 1 :(得分:14)

传递jq,就像sed for JSON一样。 E.g:

jq . file.log

答案 2 :(得分:4)

为什么不通过JSON formatter on the command line运行它?

e.g。 (例子来自上面的链接)

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

另一种方法可能是围绕上述工具(或许可能)jq构建一个shell脚本来执行一些自定义堆栈跟踪解析

答案 3 :(得分:2)

如果您使用Keen.IO - 他们的CLI工具可以上传以行为准的JSON,那么您可以使用他们的“资源管理器”#39;过滤/查看日志事件。

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

答案 4 :(得分:2)

您应该尝试winston-logs-display

演示输出:

winston-logs-display output

此外Log.io也是不错的选择。它支持winston日志。

答案 5 :(得分:2)

我知道我迟到了,但有一个简单的方法。

  1. 在浏览器中打开一个新标签
  2. 打开开发工具
  3. 运行此代码
const entries = prompt('Log Data').split('\n').map(JSON.parse);

console.table(entries);

在提示中输入您的日志数据,然后按 Enter。

现在您拥有日志条目的交互式表格视图。您可以单击标题进行排序,甚至可以右键单击某些选项。您还可以调整列的大小。

要查看某些条目,请执行

console.log(entries.filter(entry => entry.foo.bar > 323)); // Just an example, you can do anything with the array

全屏查看 devtools 以查看更多数据。

答案 6 :(得分:1)

似乎节点bunyan具有的功能可让您以人类可读的方式使用CLI过滤和查看json日志。

$ 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

答案 7 :(得分:1)

它很慢,但你的shell可以做到这一点,格式化,着色JSON。

./thing | ndjson

asciicast

如何?

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

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

对于fish,语法为

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

为了让它更加华丽,只需pip install pygments

定义一个方便的别名pp,以便您运行cat file.json | pp

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

然后定义ndjson

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

现在,您可以键入以下内容以获取格式化,着色的JSON。

./thing | ndjson

(使用funcedfuncsavefish中定义别名

答案 8 :(得分:1)

如果有帮助(在上次响应后已有很多年),我合成了单行命令,该命令甚至允许python 2.7一次获取tail -f winston.log的实时一行输出并通过python的json.tool通过管道进行漂亮的打印。

tail -f winston.log | while read -r line; do echo -n "$line" | python -m json.tool; done

(请注意,如果您使用的是python 3.8及更高版本,则将参数--json-lines提供给json.tool时,无需使用while..do药水)。

答案 9 :(得分:0)

我使用 lnav:我在某种程度上支持 json-logs:即您可以轻松定义自己的 log-format。通过这种方式,您可以根据需要优化显示(即使其与您的 winston 日志输出相匹配。

这是我们的 lnav-log-config 的一个简单示例:

{
  "$schema": "https://lnav.org/schemas/format-v1.schema.json",
  "winston": {
    "title": "My Custom Json log format",
    "description": "Custom JSON-log format for winston",
    "url": "https://github.com/winstonjs/winston#formats",
    "file-pattern": "\\.jsonlog",
    "json": true,
    "level-field": "level",
    "timestamp-field": "timestamp",
    "body-field": "message",
    "line-format": [
      {
        "field": "__timestamp__"
      },
      " ",
      {
        "field": "__level__",
        "text-transform": "uppercase"
      },
      " ",
      {
        "field": "message"
      }
    ]
  }
}

注意:我使用 "file-pattern": "\\.jsonlog",:根据需要更改或删除它。

lnav 甚至适用于我的 WSL2。
照常安装:sudo apt install lnav

答案 10 :(得分:0)

我通过编写一个 npm 包解决了这个问题(用法:tail -f logFile.log | npx winston-log-viewer)你可以在这里看到完整的描述

https://stackoverflow.com/a/68455246/4650625