Skip to content

Sentry transport for the winston logger that using the official Sentry SDK for Javascript instead of the old Raven.

License

Notifications You must be signed in to change notification settings

benjamin658/winston-sentry-javascript-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

winston-sentry-javascript-node

winston npm version license

Sentry transport for the winson logger 3+ and uses the @sentry/node SDK instead of the old Raven.

This package is written in Typescript with the well typing and code quality.

Installation

npm install winston-sentry-javascript-node --save

Usage

import { SentryTransport } from 'winston-sentry-javascript-node';

const logger = winston.createLogger({
  transports: [
    new SentryTransport({
      sentry:{
        dsn: 'MY_SENTRY_DSN',
      },
    }),
  ],
});

logger.error('Plain text error.');
logger.error(new Error('Something went wrong.'));

Extra / Tags / User Example

Set user information, as well as tags and further extras.

logger.error('Plain text error.', {
  tags: {
    foo: 'bar',
  },
  user: {
    ip: '127.0.0.1',
    username: 'user1',
  },
  extra: {
    extra1: 'extra1',
    extra2: 'extra2',
  },
});

Handle Exception

Catch and send uncaughtException to the Sentry.

const logger = winston.createLogger({
  transports: [
    new SentryTransport({
      sentry{
        dsn: 'MY_SENTRY_DSN',
      },
      handleExceptions: true,
    }),
  ],
});

// or

const logger = winston.createLogger({
  exceptionHandlers: [
    new SentryTransport({
      sentry: {
        dsn: 'MY_SENTRY_DSN',
      }
    }),
  ]
});

Options

new SentryTransport(opts)
  • opts: TransportStreamOptions
interface TransportStreamOptions {
  format?: logform.Format;
  level?: string;
  silent?: boolean;
  handleExceptions?: boolean;

  log?(info: any, next: () => void): any;
  logv?(info: any, next: () => void): any;
  close?(): void;
}

Default Extra for Error Object

By default, if you provide an Error Object to logger, this package will set the following extra:

{
  stack: err.stack,
  message: err.message,
}

© Ben Hu (benjamin658), 2019-NOW

Released under the MIT License