Skip to content

Commit 1775246

Browse files
committed
fix(consola): Add extra keys as consola. attributes
1 parent 17b7532 commit 1775246

2 files changed

Lines changed: 68 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
@@ -194,8 +194,11 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
194194

195195
return {
196196
log(logObj: ConsolaLogObject) {
197+
// We need to exclude certain known properties from being added as additional attributes
197198
// eslint-disable-next-line @typescript-eslint/no-unused-vars
198-
const { type, level, message: consolaMessage, args, tag, date: _date, ...attributes } = logObj;
199+
const { type, level, message: consolaMessage, args, tag, date: _date, ...rest } = logObj;
200+
201+
const hasExtraLogObjKeys = Object.keys(rest).length > 0;
199202

200203
// Get client - use provided client or current client
201204
const client = providedClient || getClient();
@@ -223,6 +226,13 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
223226
}
224227
const message = messageParts.join(' ');
225228

229+
// Build attributes: `rest` properties from logObj get a "consola" prefix; base attributes added below may override
230+
const attributes: Record<string, unknown> = {};
231+
232+
for (const [key, value] of Object.entries(rest)) {
233+
attributes[`consola.${key}`] = value;
234+
}
235+
226236
// Build attributes
227237
attributes['sentry.origin'] = 'auto.log.consola';
228238

@@ -239,6 +249,15 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
239249
attributes['consola.level'] = level;
240250
}
241251

252+
// Extra keys on logObj (beyond reserved) indicate direct `reporter.log({ type, message, ...rest })`
253+
if (hasExtraLogObjKeys && args && args.length >= 1 && typeof args[0] === 'string') {
254+
// Use first 'string' arg as message
255+
const message = args[0];
256+
257+
_INTERNAL_captureLog({ level: logSeverityLevel, message, attributes });
258+
return;
259+
}
260+
242261
_INTERNAL_captureLog({
243262
level: logSeverityLevel,
244263
message,

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,51 @@ describe('createConsolaReporter', () => {
207207
expect(_INTERNAL_captureLog).toHaveBeenCalledTimes(6);
208208
});
209209
});
210+
211+
describe('direct reporter call (extra log keys)', () => {
212+
it('consola-merged: args=[message] with extra keys on logObj', () => {
213+
sentryReporter.log({
214+
type: 'log',
215+
level: 2,
216+
args: ['obj-message'],
217+
userId: 123,
218+
action: 'login',
219+
time: '2026-02-24T10:24:04.477Z',
220+
smallObj: { word: 'hi' },
221+
tag: '',
222+
});
223+
224+
const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
225+
226+
// Message from args
227+
expect(call.message).toBe('obj-message');
228+
expect(call.attributes).toMatchObject({
229+
'consola.type': 'log',
230+
'consola.level': 2,
231+
'consola.userId': 123,
232+
'consola.smallObj': { word: 'hi' },
233+
'consola.action': 'login',
234+
'consola.time': '2026-02-24T10:24:04.477Z',
235+
'sentry.origin': 'auto.log.consola',
236+
});
237+
expect(call.attributes?.['sentry.message.parameter.0']).toBeUndefined();
238+
});
239+
240+
it('direct reporter.log({ type, message, userId, sessionId }) captures custom keys with consola. prefix', () => {
241+
sentryReporter.log({
242+
type: 'info',
243+
message: 'User action',
244+
userId: 123,
245+
sessionId: 'abc-123',
246+
});
247+
248+
const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
249+
expect(call.message).toBe('User action');
250+
expect(call.attributes).toMatchObject({
251+
'consola.type': 'info',
252+
'consola.userId': 123,
253+
'consola.sessionId': 'abc-123',
254+
});
255+
});
256+
});
210257
});

0 commit comments

Comments
 (0)