Integrate node.js logging with Sentry


Effective logging is crucial to any application deployed in production. For node.js, express based applications, we use Winston as our logging framework.

In our scenario, we also want some crucial logs to be logged to Sentry.

A simple setup of Sentry is described below. This is same as what is described in sentry docs.

var sentry_enabled = false;

if (process.env.NODE_ENV == 'production') {
    sentry_enabled = true;
}

if (sentry_enabled) {
    app.use(raven.middleware.express.requestHandler(config.sentry_dsn));
}

// setup routes
app.use('/', healthcheck);

if (sentry_enabled) {
    app.use(raven.middleware.express.errorHandler(config.sentry_dsn));
}

All good uptil now, except that this setup logs only unhandled exceptions to Sentry. For sending manual log lines to Sentry, sentry recommends using client.captureException(e) or client.captureMessage(msg) as per these docs

However, it could be great if we send our winston error level logs to sentry.

That's where winston-sentry packages comes to help. It can send your winston logs to sentry automatically.

All that is needed is to add a Sentry transport to the winston logger.

var winston = require('winston');
var Sentry = require('winston-sentry');
winston.emitErrs = true;

var logger = new winston.Logger({
    transports: [
        new winston.transports.File({
            level: '0',
            filename: '/opt/logfile.log',
            handleExceptions: true,
            json: false,
            colorize: false,
            timestamp: function() { return new Date().toLocaleString('ca'); }
        }),
        new winston.transports.Console({
            level: 'info',
            handleExceptions: true,
            json: false,
            colorize: true
        }),
        new Sentry({
            level: 'error',
            dsn: config.sentry_dsn,
            tags: { key: 'value' },
            extra: { key: 'value' }
        })
    ],
    exitOnError: false
});

module.exports = logger;