代码之家  ›  专栏  ›  技术社区  ›  Zeeshan Hassan Memon

@哨兵/节点集成,将Bunyan日志调用包装为面包屑

  •  1
  • Zeeshan Hassan Memon  · 技术社区  · 7 年前

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

    链接: Import name: 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!');
    

    另外,我已经试过了 bunyan-sentry-stream 但是没有成功 @sentry/node 它只是推送条目,而不是将它们视为面包屑。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nicholas Blasgen    7 年前

    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);
            }
          }
        })()
    }
    
    推荐文章