2

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.

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

The output is:

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

saz
  • 955
  • 5
  • 15
  • 26

2 Answers2

1

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

  1. Override the hostname or set a desired hostname

Configurable while constructing the logger

  1. Change "level":30 to "level":"info"
  2. Change the format of the time json object
  3. Add additional json object for example: "attr4": "value"
  4. Is there any way to change the default json object name such as time to timestamp

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"
});

Output:

{
  "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)"
}
Rajan Panneer Selvam
  • 1,279
  • 10
  • 24
  • i went through this doc: https://www.npmjs.com/package/bunyan but haven't figured out how to do any of those while streaming json output – saz May 08 '18 at 18:06
0

This is Isaac from Peru.

Only you should add a field in the property log.fields."here your property". If you wish to change the time format you should change the time attribute like this:

log.fields.time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");

A custom property, for example:

log.fields.hola = "hola";

Only add the 'moment' npm in the header of your class.

This is an example code of my class 'logger.js'

const bunyan = require('bunyan');
const moment = require('moment');
function info(paramLog){
  let log = bunyan.createLogger({
    name: 'MyLoggerName',
    streams:[
                {
                    level:'info',
                    stream: process.stdout
                }
            ]
        
   });
   log.fields.time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
   if(paramLog && paramLog !== undefined && paramLog !==null){
     log.info(JSON.stringify(paramLog));
   }

}

module.exports = {
 info
}

The output is like this:

{"name":"MyLoggerName","hostname":"USER10","pid":14880,"time":"2021-02-17 18:04:03","level":30,"msg":"\"---end update\"","v":0}

Try it and success with your test.