Sentry-javascript: 今でも可能ですか? bunyan ログ呼び出しをパンくずリストとしてラップする @sentry/node 統合 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://stackoverflow.com/questions/53310580/sentry-node-integration-to-wrap-bunyan-log-calls-as-breadcrumbs

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

元の質問に回答しました。 まだ質問がある場合は、お気軽に再開してください。

typescript を使用している場合、上記の例で WriteableStream を提供しないとコンパイラの問題が発生する可能性があるため、以下のように Writeable コンストラクターを使用できます。

さらに:

  • Bunyan から出力されるレベルは Senty の重大度ではなく数値であるため、 level: Sentry.Severity.fromString(nameFromLevel[chunk.level])を使用して値を変換する必要があります。
  • 既存の bunyan ストリームをオーバーライドしたくないので、BunyanOptions を渡す代わりに addStream 関数を使用します
// 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 評価