Skip to content

Sentry.logger.* ignores scope parameter in non-template string case #18318

@puneet-ekline

Description

@puneet-ekline

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

10.25.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

No response

Steps to Reproduce

Environment
Package: @sentry/node (and @sentry/node-core)

Description
When using Sentry.logger.info() (or other log methods) with a custom scope in a non-template string call, the scope parameter in the metadata object is ignored. This makes it impossible to use Sentry Logs with a manual NodeClient setup in shared environments (like VS Code Extension Host).

Steps to reproduce


// Manual client setup (recommended for shared environments)
const client = new NodeClient({ dsn: '...', enableLogs: true, ... });
const isolatedScope = new Scope();
isolatedScope.setClient(client);
client.init();

// Try to log with the isolated scope
Sentry.logger.info('Test message', { key: 'value' }, { scope: isolatedScope });
// Expected: Log sent using isolatedScope's client
// Actual: Log is NOT sent (scope is ignored, falls back to global scope which has no client)

Expected Result

// Expected: Log sent using isolatedScope's client

Actual Result

// Actual: Log is NOT sent (scope is ignored, falls back to global scope which has no client)

Additional Context

Likely Root Cause (found by AI)
In @sentry/node-core/src/logs/capture.ts, the captureLog function incorrectly accesses the scope:

function captureLog(level, ...args) {
  const [messageOrMessageTemplate, paramsOrAttributes, maybeAttributesOrMetadata, maybeMetadata] = args;
  if (Array.isArray(paramsOrAttributes)) {
    // Template string case (4 args): messageTemplate, params[], attributes, metadata
    core._INTERNAL_captureLog({ ... }, maybeMetadata?.scope);  // ✅ Correct
  } else {
    // Non-template case (3 args): message, attributes, metadata
    core._INTERNAL_captureLog({ ... }, maybeMetadata?.scope);  // ❌ BUG!
  }
}
function captureLog(level, ...args) {  const [messageOrMessageTemplate, paramsOrAttributes, maybeAttributesOrMetadata, maybeMetadata] = args;  if (Array.isArray(paramsOrAttributes)) {    // Template string case (4 args): messageTemplate, params[], attributes, metadata    core._INTERNAL_captureLog({ ... }, maybeMetadata?.scope);  // ✅ Correct  } else {    // Non-template case (3 args): message, attributes, metadata    core._INTERNAL_captureLog({ ... }, maybeMetadata?.scope);  // ❌ BUG!  }}

In the non-template case:
args[2] (maybeAttributesOrMetadata) contains the metadata with { scope }
args[3] (maybeMetadata) is undefined
The code uses maybeMetadata?.scope which is undefined

Expected Fix (Recommended by AI)

} else {
  core._INTERNAL_captureLog(
    { level, message: messageOrMessageTemplate, attributes: paramsOrAttributes },
    maybeAttributesOrMetadata?.scope,  // Use the correct variable
  );
}
} else {  core._INTERNAL_captureLog(    { level, message: messageOrMessageTemplate, attributes: paramsOrAttributes },    maybeAttributesOrMetadata?.scope,  // Use the correct variable  );}

Workaround
Use _INTERNAL_captureLog directly:

import { _INTERNAL_captureLog } from '@sentry/core';

_INTERNAL_captureLog({ level: 'info', message: 'Test', attributes: {} }, isolatedScope);
import { _INTERNAL_captureLog } from '@sentry/core';_INTERNAL_captureLog({ level: 'info', message: 'Test', attributes: {} }, isolatedScope);
``

Impact
This bug prevents using Sentry Logs in any environment that requires a manual client setup with isolated scopes, such as:
VS Code extensions (shared Extension Host process)
Serverless functions with multiple tenants
Any application needing client isolation

### Priority

<sub>React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it.</sub>

Metadata

Metadata

Assignees

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions