javascript - @sentry/node 集成将 bunyan 日志调用包装为面包屑
问题描述
默认情况下,Sentry 具有集成,console.log
使其成为面包屑的一部分:
链接:导入名称:Sentry.Integrations.Console
我们如何使它也适用于bunyan logger,例如:
const koa = require('koa');
const app = new koa();
const bunyan = require('bunyan');
const log = bunyan.createLogger({
name: 'app',
..... other settings go here ....
});
const Sentry = require('@sentry/node');
Sentry.init({
dsn: MY_DSN_HERE,
integrations: integrations => {
// should anything be handled here & how?
return [...integrations];
},
release: 'xxxx-xx-xx'
});
app.on('error', (err) => {
Sentry.captureException(err);
});
// I am trying all to be part of sentry breadcrumbs
// but only console.log('foo'); is working
console.log('foo');
log.info('bar');
log.warn('baz');
log.debug('any');
log.error('many');
throw new Error('help!');
PS我已经尝试过bunyan-sentry-stream但没有成功@sentry/node,它只是推送条目而不是将它们视为面包屑。
解决方案
Bunyan 支持自定义流,而这些流只是函数调用。见https://github.com/trentm/node-bunyan#streams
下面是一个简单地写入控制台的示例自定义流。直接使用这个例子来代替写入哨兵模块,可能是调用Sentry.addBreadcrumb({})
或类似的函数。
请注意,尽管record
我下面示例中的变量是 JSON 字符串,因此您可能希望解析它以从中获取日志级别、消息和其他数据以提交给 Sentry。
{
level: 'debug',
stream:
(function () {
return {
write: function(record) {
console.log('Hello: ' + record);
}
}
})()
}
推荐阅读
- redis - ioredis 连接事件的无限循环
- javascript - 映射嵌套 JSON
- api - 从 ThingsBoard 将数据上传到 Amazon S3
- javascript - 提交表单时即使表单内容为空也会发布表单并在烧瓶中重新加载表单
- c# - 更改 DbSet 类型参数时,EF Core 的行为有所不同
- python - 为什么 PCA 结果会随着输入的微小变化而发生巨大变化?
- angular - 角度动画不应用样式 :increment :decrement
- dpkg - 如何告诉 debian 包忘记文件(这样文件在清除或删除期间不会被删除)?
- python - 重复交叉验证
- html - 在 CSS 中是否可以使用此蒙版边框切片(冻结蒙版的顶部和底部条)?