Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Sentry from '@sentry/node';
import pino from 'pino';

const logger = pino({ name: 'myapp' });

Sentry.withIsolationScope(() => {
Sentry.startSpan({ name: 'startup' }, () => {
// Omitting the message string and using the msg field instead
logger.info({ msg: 'test-msg' });
logger.info({ msg: 'test-msg-2', userId: 'user-123', action: 'login' });
logger.info('test-string');
});
});
63 changes: 63 additions & 0 deletions dev-packages/node-integration-tests/suites/pino/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,67 @@ conditionalTest({ min: 20 })('Pino integration', () => {
.start()
.completed();
});

test('captures structured logs with msg field', async () => {
const instrumentPath = join(__dirname, 'instrument.mjs');

await createRunner(__dirname, 'scenario-structured-logging.mjs')
.withMockSentryServer()
.withInstrument(instrumentPath)
.ignore('transaction')
.expect({
log: {
items: [
{
timestamp: expect.any(Number),
level: 'info',
body: 'test-msg',
trace_id: expect.any(String),
severity_number: 9,
attributes: {
name: { value: 'myapp', type: 'string' },
'pino.logger.level': { value: 30, type: 'integer' },
msg: { value: 'test-msg', type: 'string' },
'sentry.origin': { value: 'auto.log.pino', type: 'string' },
'sentry.release': { value: '1.0', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
},
},
{
timestamp: expect.any(Number),
level: 'info',
body: 'test-msg-2',
trace_id: expect.any(String),
severity_number: 9,
attributes: {
name: { value: 'myapp', type: 'string' },
'pino.logger.level': { value: 30, type: 'integer' },
msg: { value: 'test-msg-2', type: 'string' },
userId: { value: 'user-123', type: 'string' },
action: { value: 'login', type: 'string' },
'sentry.origin': { value: 'auto.log.pino', type: 'string' },
'sentry.release': { value: '1.0', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
},
},
{
timestamp: expect.any(Number),
level: 'info',
body: 'test-string',
trace_id: expect.any(String),
severity_number: 9,
attributes: {
name: { value: 'myapp', type: 'string' },
'pino.logger.level': { value: 30, type: 'integer' },
'sentry.origin': { value: 'auto.log.pino', type: 'string' },
'sentry.release': { value: '1.0', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
},
},
],
},
})
.start()
.completed();
});
});
5 changes: 3 additions & 2 deletions packages/node-core/src/integrations/pino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial<PinoOptions

const [captureObj, message, levelNumber] = args;
const level = self?.levels?.labels?.[levelNumber] || 'info';
const logMessage = message || (resultObj?.msg as string | undefined) || '';

if (enableLogs && options.log.levels.includes(level)) {
const attributes: Record<string, unknown> = {
Expand All @@ -142,7 +143,7 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial<PinoOptions
'pino.logger.level': levelNumber,
};

_INTERNAL_captureLog({ level, message, attributes });
_INTERNAL_captureLog({ level, message: logMessage, attributes });
}

if (options.error.levels.includes(level)) {
Expand All @@ -167,7 +168,7 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial<PinoOptions
return;
}

captureMessage(message, captureContext);
captureMessage(logMessage, captureContext);
});
}
}
Expand Down