gpt4 book ai didi

node.js - 是否可以覆盖 bunyan 的日志功能?

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:12 25 4
gpt4 key购买 nike

我正在使用 bunyan.js 作为我的日志记录解决方案,我想为其日志记录功能添加功能。

例如,我想在每次调用 log.fatal() 时向第三方 API 发送一些内容

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

谢谢!

最佳答案

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

您可以在创建记录器对象时指定额外的流,或者使用 addStream 方法(在他们的自述文件中没有提到,但它是一个公共(public)方法,记录器也在内部使用),例如:

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

当然,如果您使用的任何东西(某些数据狗库?)为您提供接受编写 JS 对象的可写流,则不需要创建自己的包装器。

关于node.js - 是否可以覆盖 bunyan 的日志功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37527251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com