Sentry-javascript: 现在有可能吗? @sentry/node 集成将 bunyan 日志调用包装为面包屑 SO 链接:https://stackoverflow.com/questions/53310580/sentry-node-integration-to-wrap-bunyan-log-calls-as-breadcrumbs

创建于 2018-11-15  ·  3评论  ·  资料来源: getsentry/sentry-javascript

如何进行 @sentry/node 集成以将 bunyan 日志调用包装为面包屑?

包裹

@sentry/node 4.3.0

描述

Sentry 默认情况下集成了console.log以使其成为面包屑的一部分:

链接:导入名称:Sentry.Integrations.Console

我们如何使它也适用于bunyan 记录器,例如:

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 ,它只是推送条目而不是将它们视为面包屑。

SO链接: https :

Question

最有用的评论

https://docs.sentry.io/enriching-error-data/breadcrumbs/?platform=browser + 因为有人已经在 SO 上回答了:

var log = bunyan.createLogger({
  name: 'myapp',
  streams: [{
  level: 'debug',
  stream: {
    write: function(record) {
      Sentry.addBreadcrumb({
        message: record.msg,
        level: record.level
      });
    }
  }]
});

所有3条评论

https://docs.sentry.io/enriching-error-data/breadcrumbs/?platform=browser + 因为有人已经在 SO 上回答了:

var log = bunyan.createLogger({
  name: 'myapp',
  streams: [{
  level: 'debug',
  stream: {
    write: function(record) {
      Sentry.addBreadcrumb({
        message: record.msg,
        level: record.level
      });
    }
  }]
});

原问题已回答。 如果您还有更多问题,请随时重新打开。

如果您正在使用打字稿,您可能会因未提供上述示例的 WriteableStream 而遇到一些编译器问题,因此您可以使用如下所示的 Writeable 构造函数。

此外:

  • Bunyan 发出的级别是一个数字,而不是 Senty 严重性,因此您必须使用level: Sentry.Severity.fromString(nameFromLevel[chunk.level])转换该值
  • 我不想覆盖现有的 bunyan 流,所以使用 addStream 函数而不是传入 BunyanOptions
// if you want some stronger typing around the bunyan log entry
interface BunyanChunk {
  pid?: number;
  level: number;
  msg: string
  // ...   hostname?: string;
  [custom: string]: unknown;
}

const appLogger = Bunyan.createLogger();

appLogger.addStream({
  level: 'debug',
  // use the Writable constructor and pass a write function
  stream: new Writable({
    write(c: string, encoding, next) {
      const chunk: BunyanChunk = JSON.parse(c);
      Sentry.addBreadcrumb({
        message: chunk.msg,
        // convert from a Bunyan level to a Sentry Severity
        level: Sentry.Severity.fromString(Bunyan.nameFromLevel[chunk.level]),
      });
      next();
    },
  }),
});
此页面是否有帮助?
0 / 5 - 0 等级