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

901 views Asked by At

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.

1

There are 1 answers

0
Nicholas Blasgen On BEST ANSWER

Bunyan supports custom streams, and those streams are just function calls. See 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.

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.

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