【问题标题】:Is there a way to show winston log files like we show it in bunyan CLI?有没有办法像我们在 bunyan CLI 中显示的那样显示 winston 日志文件?
【发布时间】:2021-09-27 07:52:55
【问题描述】:

Bunyan logger 我们可以看到这样的日志文件: tail -f sample.log | bunyan 并以彩色显示日志并漂亮地显示 json 对象,但我在 Winston 记录器中找不到类似该解决方案的想法,任何人对此有想法吗?

【问题讨论】:

标签: node.js logging tail winston bunyan


【解决方案1】:

我写了一个基于 Bunyan CLI 的小型 npm 包,用于漂亮地打印 winston 日志,你可以这样使用它:

  • npm i -g winston-log-viewer
  • tail -f logFile.log | winston-log-viewer

或者

  • tail -f logFile.log | npx winston-log-viewer

https://github.com/mohammadranjbarz/winston-log-viewer

【讨论】:

  • 一个不错的 json 日志可视化项目。
【解决方案2】:

类似于我创建的winston-log-viewer munia-pretty-json 并将其用于许多项目。您可以在控制台中可视化任何 json 日志。

npm install -g 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

或者tail json文件:

tail -f app-log.json | munia-pertty-json

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

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

使用模板:

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

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 2017-11-08
    • 2011-10-25
    • 2020-12-24
    • 1970-01-01
    • 2016-05-23
    • 2015-10-03
    • 2013-08-05
    相关资源
    最近更新 更多