是否有可能重写bunyan的日志function?

我使用bunyan.js作为我的日志logging解决scheme,我想为它的日志function添加function。

例如,我想每次有一个调用log.fatal()时发送一些东西给第三方API

那可能吗? 我查看了文档,没有看到任何可以注册的事件或其他解决scheme。

谢谢!

Bunyan有它的“stream”: https : //github.com/trentm/node-bunyan#streams每个logging器可以写入多个stream。

您可以在创buildlogging器对象时指定其他stream,也可以使用addStream方法(在自述文件中未提及,但是它是公共方法,logging器也在内部使用),例如:

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

接着:

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

当然,如果你使用的任何东西(一些datadog库?)给你写可以写入JS对象的stream,就不需要创build自己的包装了。