This module provides a 'transport' for pino that sends errors to Sentry.
npm i @sentry/node pino-sentry-transport
import pino from "pino";
const logger = pino({
transport: {
target: "pino-sentry-transport",
options: {
sentry: {
dsn: "https://<key>:<secret>@sentry.io/<project>",
// additional options for sentry
},
withLogRecord: true, // default false - send the log record to sentry as a context.(if its more then 8Kb Sentry will throw an error)
tags: ['id'], // sentry tags to add to the event, uses lodash.get to get the value from the log record
context: ['hostname'], // sentry context to add to the event, uses lodash.get to get the value from the log record,
minLevel: 40, // which level to send to sentry
expectPinoConfig: true, // default false - pass true if pino configured with custom messageKey or errorKey see below
}
},
});
if log contain error, it will send to sentry using captureException if not it will use captureMessage.
Because Pino transport runs in a separate worker thread, Sentry needs to be initialized again in the worker. usually it means Sentry need to be initialized twice
- in the application code
- in pino-sentry-transport
expectPinoConfig is passed to pino-abstract-transport to allow Pino to pass the messageKey and errorKey to the transport pino-abstract-transport