Skip to content

Commit a90aade

Browse files
committed
fix(consola): Add extra keys as consola. attributes
1 parent 365f7fa commit a90aade

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

packages/core/src/integrations/consola.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface ConsolaReporter {
6161
*/
6262
export interface ConsolaLogObject {
6363
/**
64-
* Allows additional custom properties to be set on the log object.
64+
* Allows additional custom properties to be set on the log object (e.g. when reporter is called directly)
6565
* These properties will be captured as log attributes with a 'consola.' prefix.
6666
*
6767
* @example
@@ -187,8 +187,11 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
187187

188188
return {
189189
log(logObj: ConsolaLogObject) {
190+
// We need to exclude certain known properties from being added as additional attributes
190191
// eslint-disable-next-line @typescript-eslint/no-unused-vars
191-
const { type, level, message: consolaMessage, args, tag, date: _date, ...attributes } = logObj;
192+
const { type, level, message: consolaMessage, args, tag, date: _date, ...rest } = logObj;
193+
194+
const hasExtraLogObjKeys = Object.keys(rest).length > 0;
192195

193196
// Get client - use provided client or current client
194197
const client = providedClient || getClient();
@@ -216,6 +219,13 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
216219
}
217220
const message = messageParts.join(' ');
218221

222+
// Build attributes: `rest` properties from logObj get a "consola" prefix; base attributes added below may override
223+
const attributes: Record<string, unknown> = {};
224+
225+
for (const [key, value] of Object.entries(rest)) {
226+
attributes[`consola.${key}`] = value;
227+
}
228+
219229
// Build attributes
220230
attributes['sentry.origin'] = 'auto.log.consola';
221231

@@ -232,6 +242,15 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
232242
attributes['consola.level'] = level;
233243
}
234244

245+
// Extra keys on logObj (beyond reserved) indicate direct `reporter.log({ type, message, ...rest })`
246+
if (hasExtraLogObjKeys && args && args.length >= 1 && typeof args[0] === 'string') {
247+
// Use first 'string' arg as message
248+
const message = args[0];
249+
250+
_INTERNAL_captureLog({ level: logSeverityLevel, message, attributes });
251+
return;
252+
}
253+
235254
_INTERNAL_captureLog({
236255
level: logSeverityLevel,
237256
message,

packages/core/test/lib/integrations/consola.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ vi.mock('../../../src/currentScopes', () => ({
2222

2323
describe('createConsolaReporter', () => {
2424
let mockClient: TestClient;
25+
let sentryReporter: ReturnType<typeof createConsolaReporter>;
2526

2627
beforeEach(() => {
2728
vi.clearAllMocks();
@@ -40,6 +41,7 @@ describe('createConsolaReporter', () => {
4041

4142
vi.mocked(getClient).mockReturnValue(mockClient);
4243
vi.mocked(getCurrentScope).mockReturnValue(mockScope as any);
44+
sentryReporter = createConsolaReporter();
4345
});
4446

4547
afterEach(() => {
@@ -307,4 +309,51 @@ describe('createConsolaReporter', () => {
307309
expect(_INTERNAL_captureLog).toHaveBeenCalledTimes(6);
308310
});
309311
});
312+
313+
describe('direct reporter call (extra log keys)', () => {
314+
it('consola-merged: args=[message] with extra keys on logObj', () => {
315+
sentryReporter.log({
316+
type: 'log',
317+
level: 2,
318+
args: ['obj-message'],
319+
userId: 123,
320+
action: 'login',
321+
time: '2026-02-24T10:24:04.477Z',
322+
smallObj: { word: 'hi' },
323+
tag: '',
324+
});
325+
326+
const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
327+
328+
// Message from args
329+
expect(call.message).toBe('obj-message');
330+
expect(call.attributes).toMatchObject({
331+
'consola.type': 'log',
332+
'consola.level': 2,
333+
'consola.userId': 123,
334+
'consola.smallObj': { word: 'hi' },
335+
'consola.action': 'login',
336+
'consola.time': '2026-02-24T10:24:04.477Z',
337+
'sentry.origin': 'auto.log.consola',
338+
});
339+
expect(call.attributes?.['sentry.message.parameter.0']).toBeUndefined();
340+
});
341+
342+
it('direct reporter.log({ type, message, userId, sessionId }) captures custom keys with consola. prefix', () => {
343+
sentryReporter.log({
344+
type: 'info',
345+
message: 'User action',
346+
userId: 123,
347+
sessionId: 'abc-123',
348+
});
349+
350+
const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
351+
expect(call.message).toBe('User action');
352+
expect(call.attributes).toMatchObject({
353+
'consola.type': 'info',
354+
'consola.userId': 123,
355+
'consola.sessionId': 'abc-123',
356+
});
357+
});
358+
});
310359
});

0 commit comments

Comments
 (0)