简体   繁体   English

@ sentry / node集成将Bunyan日志调用包装为面包屑

[英]@sentry/node integration to wrap bunyan log calls as breadcrumbs

Sentry by defaults has integration for console.log to make it part of breadcrumbs: 默认情况下,Sentry具有console.log集成,使其成为面包屑的一部分:

Link: Import name: Sentry.Integrations.Console 链接: 导入名称:Sentry.Integrations.Console

How can we make it to work for bunyan logger as well, like: 我们如何使其也适用于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 I have already tried bunyan-sentry-stream but no success with @sentry/node , it just pushes entries instead of treating them as breadcrumbs. PS我已经尝试过bunyan-sentry-stream,但是@ sentry / node并没有成功,它只是推送条目而不是将它们视为面包屑。

Bunyan supports custom streams, and those streams are just function calls. Bunyan支持自定义流,这些流只是函数调用。 See https://github.com/trentm/node-bunyan#streams 参见https://github.com/trentm/node-bunyan#streams

Below is an example custom stream that simply writes to the console. 以下是简单写入控制台的示例自定义流。 It would be straight forward to use this example to instead write to the Sentry module, likely calling Sentry.addBreadcrumb({}) or similar function. 直接使用此示例代替写到Sentry模块,可能会调用Sentry.addBreadcrumb({})或类似函数。

Please note though that the variable record in my example below is a JSON string, so you would likely want to parse it to get the log level, message, and other data out of it for submission to Sentry. 请注意,尽管在我下面的示例中,变量record是一个JSON字符串,所以您可能希望对其进行解析,以获取日志级别,消息和其他数据,以提交给Sentry。

{
  level: 'debug',
  stream:
    (function () {
      return {
        write: function(record) {
          console.log('Hello: ' + record);
        }
      }
    })()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM