Skip to content

Commit aefd5fe

Browse files
committed
rework consola
1 parent 35c34e1 commit aefd5fe

2 files changed

Lines changed: 340 additions & 876 deletions

File tree

packages/core/src/integrations/consola.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ interface ConsolaReporterOptions {
8686
* });
8787
* ```
8888
*/
89-
extractAttributes?: (args: unknown[]) => ExtractAttributesResult | null | undefined;
89+
// `any` is the type that consola provides for log arguments
90+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91+
extractAttributes?: (args: any[]) => ExtractAttributesResult | null | undefined;
9092
}
9193

9294
export interface ConsolaReporter {
@@ -111,8 +113,7 @@ export interface ConsolaReporter {
111113
*/
112114
export interface ConsolaLogObject {
113115
/**
114-
* Allows additional custom properties to be set on the log object.
115-
* todo: they are not prefixed?
116+
* Allows additional custom properties to be set on the log object when reporter is called directly.
116117
* These properties will be captured as log attributes with a 'consola.' prefix.
117118
*
118119
* @example
@@ -124,7 +125,6 @@ export interface ConsolaLogObject {
124125
* userId: 123,
125126
* sessionId: 'abc-123'
126127
* });
127-
* todo: NOOO it does not?
128128
* // Will create attributes: consola.userId and consola.sessionId
129129
* ```
130130
*/
@@ -198,7 +198,9 @@ export interface ConsolaLogObject {
198198
* When provided, this is the final formatted message. When not provided,
199199
* the message should be constructed from the `args` array.
200200
*
201-
* In reporters, this is probably always undefined: https://github.com/unjs/consola/issues/406#issuecomment-3684792551
201+
* Note: In reporters, `message` is typically undefined. It is primarily for
202+
* `consola.[type]({ message: 'xxx' })` usage and is normalized into `args` before
203+
* reporters receive the log object. See: https://github.com/unjs/consola/issues/406#issuecomment-3684792551
202204
*/
203205
message?: string;
204206
}
@@ -232,7 +234,7 @@ function extractStructuredAttributes(
232234
// Extract attributes from first arg
233235
const attributes = normalize(firstArg, normalizeDepth, normalizeMaxBreadth) as Record<string, unknown>;
234236

235-
// Determine message (second arg if string, otherwise empty)
237+
// Determine message (second arg if 'string', otherwise empty)
236238
const secondArg = args[1];
237239
const message = typeof secondArg === 'string' ? secondArg : '';
238240

@@ -276,8 +278,7 @@ function processArgsFallbackMode(
276278

277279
if (followingArgs.length > 0 && typeof firstArg === 'string' && !hasConsoleSubstitutions(firstArg)) {
278280
const templateAttrs = createConsoleTemplateAttributes(firstArg, followingArgs);
279-
for (const key in templateAttrs) {
280-
const value = templateAttrs[key];
281+
for (const [key, value] of Object.entries(templateAttrs)) {
281282
messageAttributes[key] = key.startsWith('sentry.message.parameter.')
282283
? normalize(value, normalizeDepth, normalizeMaxBreadth)
283284
: value;
@@ -312,10 +313,10 @@ function processStructuredMode(
312313

313314
// Add extracted attributes, but don't override existing or consola-prefixed attributes
314315
if (extractedAttrs) {
315-
for (const key in extractedAttrs) {
316+
for (const [key, value] of Object.entries(extractedAttrs)) {
316317
// Only add if not conflicting with existing or consola-prefixed attributes
317318
if (!(key in attributes) && !(`consola.${key}` in attributes)) {
318-
attributes[key] = extractedAttrs[key];
319+
attributes[key] = value;
319320
}
320321
}
321322
}
@@ -365,14 +366,18 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
365366
const providedClient = options.client;
366367

367368
return {
369+
// eslint-disable-next-line complexity
368370
log(logObj: ConsolaLogObject) {
369-
const { type, level, message: consolaMessage, args, tag, date: _date, ...rest } = logObj;
371+
const { type, level, message: consolaMessage, args, tag, ...rest } = logObj;
370372

371-
// Extra keys on logObj (beyond reserved) indicate consola merged a single object, e.g. consola.log({ message: "x", userId: 1 })
373+
// Extra keys on logObj (beyond reserved) indicate direct `reporter.log({ type, message, ...rest })`
372374
const hasExtraKeys = Object.keys(rest).length > 0;
373375

374-
// Build attributes: extra keys first, then add reserved base attributes
375-
const attributes: Record<string, unknown> = { ...rest };
376+
// Build attributes: custom properties from logObj get a consola. prefix; base attributes added below may override
377+
const attributes: Record<string, unknown> = {};
378+
for (const [key, value] of Object.entries(rest)) {
379+
attributes[`consola.${key}`] = value;
380+
}
376381

377382
// Get client - use provided client or current client
378383
const client = providedClient || getClient();
@@ -405,7 +410,7 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
405410
attributes['consola.level'] = level;
406411
}
407412

408-
// Consola-merged: single object was spread by consola (e.g. consola.log({ message: "inline-message", userId, action }))
413+
// Direct Reporter Log: E.g. reporter.log({ message: "inline-message", userId, action })
409414
if (hasExtraKeys && args && args.length >= 1 && typeof args[0] === 'string') {
410415
const message = args[0];
411416
_INTERNAL_captureLog({

0 commit comments

Comments
 (0)