Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible now? @sentry/node integration to wrap bunyan log calls as breadcrumbs SO link: https://stackoverflow.com/questions/53310580/sentry-node-integration-to-wrap-bunyan-log-calls-as-breadcrumbs #1736

Closed
zishon89us opened this issue Nov 15, 2018 · 3 comments

Comments

@zishon89us
Copy link

How can I make @sentry/node integration to wrap bunyan log calls as breadcrumbs ?

Package

@sentry/node 4.3.0

Description

Sentry by defaults has integration for console.log to make it part of breadcrumbs:

Link: Import name: Sentry.Integrations.Console

How can we make it to work for bunyan logger as well, like:

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!');

P.S. I have already tried bunyan-sentry-stream but no success with @sentry/node, it just pushes entries instead of treating them as breadcrumbs.

SO link: https://stackoverflow.com/questions/53310580/sentry-node-integration-to-wrap-bunyan-log-calls-as-breadcrumbs

@kamilogorek
Copy link
Contributor

https://docs.sentry.io/enriching-error-data/breadcrumbs/?platform=browser + as someone already answered on the SO:

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

@kamilogorek
Copy link
Contributor

Original question was answered. Feel free to reopen if you still have more questions.

@bhishp
Copy link

bhishp commented Oct 2, 2020

If you are using typescript you might get some compiler issues for not providing a WriteableStream with the example above, therefore you can use the Writeable constructor as below.

Additionally:

  • the level emitted from Bunyan is a number, not a Senty severity, therefore you have to convert the value using level: Sentry.Severity.fromString(nameFromLevel[chunk.level])
  • I don't want to override the existing bunyan stream so use the addStream function instead of passing in 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();
    },
  }),
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants