Skip to content

Allow customization of level mapping in createSentryWinstonTransport #18868

@mnow-cd

Description

@mnow-cd

Problem Statement

Currently the createSentryWinstonTransport function uses a hardcoded mapping between Winston levels and Sentry severity levels with the internal constant.

When using custom levels in Winston that match Sentry's own naming convention, fatal or trace levels will be mapped to info because they don't exist in the internal WINSTON_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP.

const SentryTransport = Sentry.createSentryWinstonTransport(WinstonTransport);

const logger = createLogger({
  levels: {
    fatal: 0,
    error: 1,
    warn: 2,
    info: 3,
    debug: 4,
    trace: 5
  },
  transports: [new SentryTransport]
});

logger.info('msg'); // logged to Sentry as 'info'
logger.fatal('msg'); // logged to Sentry also as 'info'

Solution Brainstorm

Add an optional levelMap or mapLevel function to the transport options that allows to override the default mapping.

const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport);

// level map
const transport = new SentryWinstonTransport({
  levelMap: {
    emergency: 'fatal',
    alert: 'error',
    custom_level: 'debug'
  }
});

// map function
new SentryWinstonTransport({
  mapLevel: (winstonLevel) => {
    if (winstonLevel === 'fatal') return 'fatal';
    return 'trace';
  }
});

Also it would be really helpful if Sentry provided constants to simplify the setup of a Winston logger to be compatible with Sentry levels, instead of having to manually align them.

const SentryTransport = Sentry.createSentryWinstonTransport(WinstonTransport);

const logger = winston.createLogger({
  // native Sentry levels for Winston
  levels: Sentry.winstonLogLevels, 
  transports: [
    new SentryTransport({
      // pre-defined mapping
      levelMap: Sentry.winstonTransportLevelMap 
    })
  ]
});

Additional Context

No response

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

Labels

External DependencyTo close this issue, an external dependency needs to be adjusted/fixed

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions