node.js – 默认情况下如何在bunyan中设置’short’输出模式

前端之家收集整理的这篇文章主要介绍了node.js – 默认情况下如何在bunyan中设置’short’输出模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
bunyan中的常规(原始)日志如下所示:
$node myapp.js
{"name":"myapp","hostname":"myhost","pid":34572,"level":30,"msg":"start","time":"2013-01-04T07:47:25.814Z","v":0}
{"name":"myapp","widget_type":"wuzzle","msg":"creating a wuzzle","time":"2013-01-04T07:47:25.815Z","v":0}

可以使用CLI通过管道日志到bunyan -o short来使用“短”输出模式

$node myapp.js  | bunyan -o short
07:46:42.707Z  INFO myapp: start
07:46:42.709Z  INFO myapp: creating a wuzzle (widget_type=wuzzle)

默认情况下是否可以使用“短”模式,因此节点myapp.js会产生短版本的日志?

解决方法

您可以将自定义原始流传递给bunyan并编写自己的漂亮打印机:
var Logger = require('bunyan'),Stream = require('stream')

  var stream = new Stream()
  stream.writable = true

  stream.write = function(obj) {
     // pretty-printing your message
     console.log(obj.msg)
  }

  var logger = new Logger({
     name: 'foo',streams: [{
        type: "raw",stream: stream,}],serializers: {
        err: Logger.stdSerializers.err,req: Logger.stdSerializers.req,res: Logger.stdSerializers.res,},})

  // -------------------------------

  logger.info('hello world')

您可以查看完成此工作的this filepretty-print所有消息.

原文链接:https://www.f2er.com/nodejs/241292.html

猜你在找的Node.js相关文章