Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions dev-packages/node-integration-tests/suites/winston/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,44 @@ async function run(): Promise<void> {
});
}

// If custom level mapping is requested
if (process.env.CUSTOM_LEVEL_MAPPING === 'true') {
const customLevels = {
levels: {
customCritical: 0,
customWarning: 1,
customNotice: 2,
},
};

const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
customLevelMap: {
customCritical: 'fatal',
customWarning: 'warn',
customNotice: 'info',
},
});

const mappedLogger = winston.createLogger({
levels: customLevels.levels,
// https://github.com/winstonjs/winston/issues/1491
// when custom levels are set with a transport,
// the level must be set on the logger
level: 'customNotice',
transports: [new SentryWinstonTransport()],
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error - custom levels are not part of the winston logger
mappedLogger.customCritical('This is a critical message');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error - custom levels are not part of the winston logger
mappedLogger.customWarning('This is a warning message');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error - custom levels are not part of the winston logger
mappedLogger.customNotice('This is a notice message');
}

await Sentry.flush();
}

Expand Down
91 changes: 91 additions & 0 deletions dev-packages/node-integration-tests/suites/winston/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
cleanupChildProcesses();
});

test('should capture winston logs with default levels', async () => {

Check failure on line 9 in dev-packages/node-integration-tests/suites/winston/test.ts

View workflow job for this annotation

GitHub Actions / Node (24) (TS 3.8) Integration Tests

suites/winston/test.ts > winston integration > should capture winston logs with default levels

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/winston/test.ts:9:3
const runner = createRunner(__dirname, 'subject.ts')
.expect({
log: {
Expand Down Expand Up @@ -49,7 +49,7 @@
await runner.completed();
});

test('should capture winston logs with custom levels', async () => {

Check failure on line 52 in dev-packages/node-integration-tests/suites/winston/test.ts

View workflow job for this annotation

GitHub Actions / Node (24) (TS 3.8) Integration Tests

suites/winston/test.ts > winston integration > should capture winston logs with custom levels

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/winston/test.ts:52:3
const runner = createRunner(__dirname, 'subject.ts')
.withEnv({ CUSTOM_LEVELS: 'true' })
.expect({
Expand Down Expand Up @@ -123,7 +123,7 @@
await runner.completed();
});

test('should capture winston logs with metadata', async () => {

Check failure on line 126 in dev-packages/node-integration-tests/suites/winston/test.ts

View workflow job for this annotation

GitHub Actions / Node (24) (TS 3.8) Integration Tests

suites/winston/test.ts > winston integration > should capture winston logs with metadata

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/winston/test.ts:126:3
const runner = createRunner(__dirname, 'subject.ts')
.withEnv({ WITH_METADATA: 'true' })
.expect({
Expand Down Expand Up @@ -183,4 +183,95 @@

await runner.completed();
});

test('should map custom winston levels to Sentry severity levels', async () => {

Check failure on line 187 in dev-packages/node-integration-tests/suites/winston/test.ts

View workflow job for this annotation

GitHub Actions / Node (24) (TS 3.8) Integration Tests

suites/winston/test.ts > winston integration > should map custom winston levels to Sentry severity levels

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/winston/test.ts:187:3
const runner = createRunner(__dirname, 'subject.ts')
.withEnv({ CUSTOM_LEVEL_MAPPING: 'true' })
.expect({
log: {
items: [
// First, the default logger captures info and error
{
timestamp: expect.any(Number),
level: 'info',
body: 'Test info message',
severity_number: expect.any(Number),
trace_id: expect.any(String),
attributes: {
'sentry.origin': { value: 'auto.log.winston', type: 'string' },
'sentry.release': { value: '1.0.0', type: 'string' },
'sentry.environment': { value: 'test', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
'server.address': { value: expect.any(String), type: 'string' },
},
},
{
timestamp: expect.any(Number),
level: 'error',
body: 'Test error message',
severity_number: expect.any(Number),
trace_id: expect.any(String),
attributes: {
'sentry.origin': { value: 'auto.log.winston', type: 'string' },
'sentry.release': { value: '1.0.0', type: 'string' },
'sentry.environment': { value: 'test', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
'server.address': { value: expect.any(String), type: 'string' },
},
},
// Then the mapped logger uses custom level mappings
{
timestamp: expect.any(Number),
level: 'fatal', // 'critical' maps to 'fatal'
body: 'This is a critical message',
severity_number: expect.any(Number),
trace_id: expect.any(String),
attributes: {
'sentry.origin': { value: 'auto.log.winston', type: 'string' },
'sentry.release': { value: '1.0.0', type: 'string' },
'sentry.environment': { value: 'test', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
'server.address': { value: expect.any(String), type: 'string' },
},
},
{
timestamp: expect.any(Number),
level: 'warn', // 'warning' maps to 'warn'
body: 'This is a warning message',
severity_number: expect.any(Number),
trace_id: expect.any(String),
attributes: {
'sentry.origin': { value: 'auto.log.winston', type: 'string' },
'sentry.release': { value: '1.0.0', type: 'string' },
'sentry.environment': { value: 'test', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
'server.address': { value: expect.any(String), type: 'string' },
},
},
{
timestamp: expect.any(Number),
level: 'info', // 'notice' maps to 'info'
body: 'This is a notice message',
severity_number: expect.any(Number),
trace_id: expect.any(String),
attributes: {
'sentry.origin': { value: 'auto.log.winston', type: 'string' },
'sentry.release': { value: '1.0.0', type: 'string' },
'sentry.environment': { value: 'test', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
'server.address': { value: expect.any(String), type: 'string' },
},
},
],
},
})
.start();

await runner.completed();
});
});
20 changes: 19 additions & 1 deletion packages/node-core/src/integrations/winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ interface WinstonTransportOptions {
* ```
*/
levels?: Array<LogSeverityLevel>;

/**
* Use this option to map custom levels to Sentry log severity levels.
*
* @example
* ```ts
* const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
* customLevelMap: {
* myCustomLevel: 'info',
* customError: 'error',
* },
* });
* ```
*/
customLevelMap?: Record<string, LogSeverityLevel>;
}

/**
Expand Down Expand Up @@ -85,7 +100,10 @@ export function createSentryWinstonTransport<TransportStreamInstance extends obj
attributes[MESSAGE_SYMBOL] = undefined;
attributes[SPLAT_SYMBOL] = undefined;

const logSeverityLevel = WINSTON_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP[levelFromSymbol as string] ?? 'info';
const customLevel = sentryWinstonOptions?.customLevelMap?.[levelFromSymbol as string];
Copy link
Member

@nicohrubec nicohrubec Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: would it make sense to warn the user in case he provides a sentry level that does not exist?

const logSeverityLevel =
customLevel ?? WINSTON_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP[levelFromSymbol as string] ?? 'info';

if (this._levels.has(logSeverityLevel)) {
captureLog(logSeverityLevel, message as string, {
...attributes,
Expand Down
Loading