Skip to content

feat(core): Extract objects as structured logs in consoleLoggingIntegration - #21385

Merged
s1gr1d merged 5 commits into
developfrom
sig/console-log-logs
Jun 9, 2026
Merged

feat(core): Extract objects as structured logs in consoleLoggingIntegration#21385
s1gr1d merged 5 commits into
developfrom
sig/console-log-logs

Conversation

@s1gr1d

Copy link
Copy Markdown
Member

Aligns the console integration so object-first logs are structured, matching the consola integration behavior. If the first argument is an object, the keys are added as log attributes.

Reference PR (consola): #19534

The universal principle is this:

IF first_argument is plain_object:
attributes ← first_argument keys/values
message ← second_argument (if string) OR empty
remaining_args → sentry.message.parameter.{0, 1, 2...}
ELSE:
message ← formatted(all_args)
args[1:] → sentry.message.parameter.{0, 1, 2...}

Example Snippet

console.warn({component: 'PaymentService',retryCount: 3},500,'TIMEOUT');console.info({userId: 42,action: 'purchase'},'Console [OBJ-FIRST+PARAMS]: Checkout complete',testOrder.orderId,testOrder.total,);

Before

image

After

image

Comment threadpackages/core/src/logs/console-integration.ts
@github-actions

github-actionsBot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser27.39 kB--
@sentry/browser - with treeshaking flags25.82 kB--
@sentry/browser (incl. Tracing)45.68 kB--
@sentry/browser (incl. Tracing + Span Streaming)47.92 kB--
@sentry/browser (incl. Tracing, Profiling)50.48 kB--
@sentry/browser (incl. Tracing, Replay)84.9 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags74.51 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)89.6 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)102.28 kB--
@sentry/browser (incl. Feedback)44.55 kB--
@sentry/browser (incl. sendFeedback)32.19 kB--
@sentry/browser (incl. FeedbackAsync)37.3 kB--
@sentry/browser (incl. Metrics)28.46 kB--
@sentry/browser (incl. Logs)28.69 kB--
@sentry/browser (incl. Metrics & Logs)29.39 kB--
@sentry/react29.18 kB--
@sentry/react (incl. Tracing)47.98 kB--
@sentry/vue32.4 kB--
@sentry/vue (incl. Tracing)47.57 kB--
@sentry/svelte27.41 kB--
CDN Bundle29.78 kB--
CDN Bundle (incl. Tracing)48.16 kB--
CDN Bundle (incl. Logs, Metrics)31.32 kB+0.29%+89 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)49.48 kB+0.18%+84 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)70.61 kB+0.11%+75 B 🔺
CDN Bundle (incl. Tracing, Replay)85.51 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)86.75 kB+0.07%+56 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)91.34 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)92.6 kB+0.09%+79 B 🔺
CDN Bundle - uncompressed88.46 kB--
CDN Bundle (incl. Tracing) - uncompressed145.67 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed93.17 kB+0.24%+216 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed149.65 kB+0.15%+216 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed217.99 kB+0.1%+216 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed264.54 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed268.5 kB+0.09%+216 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed278.24 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed282.19 kB+0.08%+216 B 🔺
@sentry/nextjs (client)50.43 kB--
@sentry/sveltekit (client)46.1 kB--
@sentry/core/server76.01 kB+0.07%+50 B 🔺
@sentry/core/browser63.14 kB+0.08%+47 B 🔺
@sentry/node-core61.73 kB+0.01%+1 B 🔺
@sentry/node130.42 kB+0.01%+1 B 🔺
@sentry/node - without tracing74.11 kB--
@sentry/aws-serverless86.29 kB+0.01%+1 B 🔺
@sentry/cloudflare (withSentry) - minified173.69 kB--
@sentry/cloudflare (withSentry)433.85 kB--

View base workflow run

@s1gr1d
s1gr1d requested review from Lms24 and chargomeJune 9, 2026 09:35

@Lms24Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for adding all of the tests! This makes it easier to understand and cover the behaviour.

Just had some remarks/questions but nothing that blocks this PR.

userId: 123,
'sentry.origin': 'auto.log.console',
'sentry.message.parameter.0': 42,
'sentry.message.parameter.1': { extra: 'data' },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

l: just to confirm, we stringify this attribute later, right?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yes, those are serialized in _INTERNAL_captureLog

expect(formatConsoleArgs).toHaveBeenCalled();
const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
expect(call.message).toBe('[1,2,3] Array data');
expect(call.attributes).not.toHaveProperty('0');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

q: why do we assert on the '0' key? Is this something we'd expect in an error case but don't want?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

In this case the log is console.info([1, 2, 3], 'Array Data') and we want to make sure that arrays are not treated as objects (on the first param). So by checking there is no 0, it means it took the fallback path and it did not attach the object keys as attributes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thx for clarifying, makes sense!

Comment on lines +259 to +279
it('falls back when first arg is an Error', () => {
const err = new Error('Test error');
triggerConsole('error', err, 'context info');

const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
expect(call.message).toContain('Error: Test error');
expect(call.message).toContain('context info');
expect(call.attributes).not.toHaveProperty('message');
expect(call.attributes).not.toHaveProperty('name');
});

it('normalizes Error objects into message parameters', () => {
const err = new Error('connection refused');
triggerConsole('error', 'Error occurred:', err);

const call = vi.mocked(_INTERNAL_captureLog).mock.calls[0]![0];
expect(call.attributes?.['sentry.message.parameter.0']).toMatchObject({
message: 'connection refused',
name: 'Error',
});
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm from a user perspective, I wouldn't understand why in the first case, my error is serialized to string and in the second case, it's extracted as an attribute. Don't wanna block this PR on it since I believe we settled on the extraction behaviour as you described in the PR description. But pointing this out as a potential discussion item to follow up on. I might also be missing something.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Talked about this offline. This is basically the same behavior as it previously was. It might make sense on first sight to treat errors as objects but it opens a can of worms if we think about how to treat error properties as log attributes. It's better if we only care for plain objects and extract only those keys as attributes.

@s1gr1d
s1gr1dforce-pushed the sig/console-log-logs branch from 025349d to 452c686CompareJune 9, 2026 11:49

@Lms24Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thx for clarifying my questions, LGTM!

@s1gr1d
s1gr1d enabled auto-merge (squash) June 9, 2026 12:44
@s1gr1d
s1gr1d merged commit 9ead55d into developJun 9, 2026
270 of 271 checks passed
@s1gr1d
s1gr1d deleted the sig/console-log-logs branch June 9, 2026 13:00
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@s1gr1d@Lms24