【问题标题】:How would a human read a json winston log file?人类如何阅读 json winston 日志文件?
【发布时间】:2014-07-02 19:29:12
【问题描述】:

对于 API、脚本和其他方面来说似乎不错。但是使用文本编辑器读取 winston json 堆栈跟踪非常困难。例如

{"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"}

【问题讨论】:

    标签: json node.js winston


    【解决方案1】:

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

    例如(上面链接的例子)

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

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

    【讨论】:

    • 感谢那位,虽然python -m json.tool 只知道如何消化一行。不过,看看结果,例如pastebin.com/16AE7Qn7 不如查看文本日志堆栈跟踪那么好。更不用说必须滚动浏览其中的一百万个。
    • 也许值得考虑围绕上述工具(或者也许)jq 构建一个 shell 脚本来执行一些自定义堆栈跟踪解析。答案已修改...
    • 从 Python 3.8 开始,json.tool 现在通过选项 --json-lines 接受 json-lines 格式,以便将每个新行解释为单独的 json 对象:docs.python.org/3/library/…
    【解决方案2】:

    只需将文件传输“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');
    

    【讨论】:

    • 只是为了澄清 - 我没有接受这个答案,因为它会从日志中删除 json。我想保留 json 日志,但使用查看器更容易阅读。
    • @ubershmekel 您现在找到解决方案了吗?我还需要在 Winston 中看到漂亮的打印日志,但以 json 格式存储。似乎 bunyan cli 有这个能力,但我不知道 Winston 是否有类似的功能。
    • 我使用bunyan 来记录它附带的命令行工具,你可以使用它的行为类似于彩色less 的json 日志。也许 cli 也适用于 winston 日志,不确定。
    【解决方案3】:

    通过jq 传递它,这就像JSON 的sed。例如:

    jq . file.log
    

    【讨论】:

    • 要跟踪日志文件,请使用tail -f file.log | jq --unbuffered .
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      似乎节点的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)
      

      【讨论】:

      • 你是对的,因此我没有接受我的回答。虽然它确实解决了我记录 json 并使用该 json 的需要。
      【解决方案6】:

      它很慢,但你的 shell 可以做到,得到格式化、彩色的 JSON。

      ./thing | ndjson
      

      怎么做?

      你在每一行上运行一些 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中定义别名)

      【讨论】:

        【解决方案7】:

        你应该试试winston-logs-display

        演示输出:

        Log.io 也是一个不错的选择。它支持winston log。

        【讨论】:

          【解决方案8】:

          如果它有帮助(在上次响应之后这么多年),我合成了单行命令,它甚至允许 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.tool 时,参数 --json-lines 应该消除对 while..do 药水的需要)。

          【讨论】:

            【解决方案9】:

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

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

            在提示中输入您的日志数据并按回车键。

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

            要查看某些条目,请执行

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

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

            【讨论】:

              【解决方案10】:

              我使用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

              【讨论】:

                【解决方案11】:

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

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

                【讨论】:

                  【解决方案12】:

                  如果你想在控制台可视化winston json 日志,你可以使用munia-pretty-json

                  你的 json 数据 (app-log.json)

                  {"time":"2021-06-09T02:50:22Z","level":"info","message":"Log for pretty JSON","module":"init","hostip":"192.168.0.138","pid":123}
                  {"time":"2021-06-09T03:27:43Z","level":"warn","message":"Here is warning message","module":"send-message","hostip":"192.168.0.138","pid":123}
                  

                  运行命令:

                  munia-pretty-json app-log.json
                  

                  这是控制台上的可读输出:

                  您可以使用模板格式化输出。默认模板为'{time} {level -c} {message}'

                  使用模板:

                  munia-pretty-json -t '{module -c} - {level} - {message}' app-log.json
                  

                  输出:

                  【讨论】:

                    猜你喜欢
                    • 2012-04-20
                    • 2019-05-20
                    • 2011-09-24
                    • 1970-01-01
                    • 2013-09-17
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-03-16
                    • 2021-04-26
                    相关资源
                    最近更新 更多