如何在Node.js Bunyan日志记录中添加自定义JSON对象? [英] How to add custom JSON objects in Node.js Bunyan Logging?

查看:87
本文介绍了如何在Node.js Bunyan日志记录中添加自定义JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Node.js Bunyan日志记录有一些疑问.我是新来的Bunyan伐木场,所以如果我问任何外行问题,请向我道歉.

I have few questions about Node.js Bunyan logging. I'm kinda new the bunyan logging, so please I apologize if i ask any laymen questions.

我正在尝试以json格式流处理bunyan日志输出.主要在文件中,我计划将其流式传输到远程主机.

I'm trying to stream bunyan log output in json format. Primarily in a file and I have plans to stream it a remote host.

这是我正在尝试的简单代码:

Here's a simple code that i'm trying:

var bunyan = require("bunyan");

var logger = bunyan.createLogger({
  name: "testApp",
  streams: [
    {
      path: "bunayan.log"
    }
  ],
  src: true
});

logger.info("Data sent to file");

输出为:

{"name":"testApp","hostname":"xxx.xxx.com","pid":14124,"level":30,"msg":"Data sent to file","time":"2018-05-07T19:14:15.866Z","src":{"file":"/path/to/file/banyan_test.js","line":11},"v":0}

所以,我正在尝试像这样格式化输出;

So, i'm trying to format the output like this;

  1. 覆盖主机名或设置所需的主机名
  2. 将级别":30更改为级别":信息"
  3. 更改时间json对象的格式
  4. 添加其他json对象,例如:"attr4":"value"
  5. 是否可以将默认的json对象名称(例如 time )更改为 timestamp
  1. Override the hostname or set a desired hostname
  2. Change "level":30 to "level":"info"
  3. Change the format of the time json object
  4. Add additional json object for example: "attr4": "value"
  5. Is there any way to change the default json object name such as time to timestamp

我找不到任何简单或清晰的示例来执行上述任何更改.谁能给我示范一些例子吗?不需要所有要点在一起,但至少需要先入为主或提供任何有用的文档.

I couldn't find any simple or clear example of doing any of the above change. Can anyone please show me some examples to start with? Doesn't need to be all points together but at least a head start or any helpful documentation.

推荐答案

查看Banyan API文档.您所有的用例都涵盖了.

Checkout the Banyan API Documentation. All your use cases are covered.

  1. 覆盖主机名或设置所需的主机名

可在构造记录器时配置

  1. 将级别":30更改为级别":信息"
  2. 更改时间json对象的格式
  3. 添加其他json对象,例如:"attr4":"value"
  4. 有什么方法可以更改默认的json对象名称,例如时间戳记时间

您可以覆盖json对象:请参见: https://github.com/trentm/node-bunyan/issues/194

You can override the json object: See this: https://github.com/trentm/node-bunyan/issues/194

var bunyan = require('bunyan');

function modifiedStream(filePath) {
  return {
    write: log => {
      log.level = bunyan.nameFromLevel[log.level];
      log.time = new Date().valueOf();
      log._timeStamp = new Date().toISOString();
      log.myProp = "Some Value" + new Date();

      var logLine = JSON.stringify(log, bunyan.safeCycles(), 2);
      console.log(logLine);
    }
  };
}

var logger = bunyan.createLogger({
  name: 'myapp',
  hostname: "My Test Mac",
  streams: [{
    type: 'raw',
    stream: modifiedStream()
  }]
});

logger.info("Hello");

logger.info({
  customProp1: "AAA",
  customProp2: "BBB"
});

输出:

{
  "name": "myapp",
  "hostname": "My Test Mac",
  "pid": 89297,
  "level": "info",
  "msg": "Hello",
  "time": 1525813632657,
  "v": 0,
  "_timeStamp": "2018-05-08T21:07:12.657Z",
  "myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}
{
  "name": "myapp",
  "hostname": "My Test Mac",
  "pid": 89297,
  "level": "info",
  "customProp1": "AAA",
  "customProp2": "BBB",
  "msg": "",
  "time": 1525813632659,
  "v": 0,
  "_timeStamp": "2018-05-08T21:07:12.659Z",
  "myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}

这篇关于如何在Node.js Bunyan日志记录中添加自定义JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆