Tzvika Mordoch
Tzvika Mordoch

Reputation: 193

Is it possible to override bunyan's log functions?

I'm using bunyan.js as my logging solution and I would like to add functionality to it's logging functions.

For example I would like to send something to a third party API each time there's a call to log.fatal()

Is that possible? I looked through the docs and I didn't see any events I can register to, or any other solution.

Thanks!

Upvotes: 2

Views: 1327

Answers (1)

ahwayakchih
ahwayakchih

Reputation: 2371

Bunyan has it's "streams": https://github.com/trentm/node-bunyan#streams Every logger can write to multiple streams.

You can either specify additional streams while creating logger object, or use addStream method (it is not mentioned in their readme, but it's a public method, that logger uses internally too), for example:

logger.addStream({
    type: 'raw',
    stream: new MyDatadog(),
    closeOnExit: true,
    // Here you specify what level should be written to this stream.
    // For example, you can use soemthing like `logger.FATAL` here.
    level: options.level
});

And then:

function MyDatadog () {
    // initialize whatever is needed here
}

MyDatadog.prototype.write = function write (record) {
    // write record data wherever you want
    // record is an object with things like:
    // - record.what
    // - record.time
    // - record.err
};

MyDatadog.prototype.end = function end () {
    // cleanup stuff here
};

Of course, creating own wrapper is not needed if whatever you use (some datadog library?) gives you writable stream which accepts writing JS objects.

Upvotes: 4

Related Questions