import { SENTRY_ORIGIN } from '@sentry/conventions/attributes'; import { getClient } from '../currentScopes'; import { addConsoleInstrumentationHandler } from '../instrument/console'; import { defineIntegration } from '../integration'; import type { ConsoleLevel } from '../types/instrument'; import type { IntegrationFn } from '../types/integration'; import { CONSOLE_LEVELS } from '../utils/debug-logger'; import { isPlainObject } from '../utils/is'; import { normalize } from '../utils/normalize'; import { captureLogEvent } from './internal'; import { createConsoleTemplateAttributes, formatConsoleArgs, hasConsoleSubstitutions } from './utils'; interface CaptureConsoleOptions { levels: ConsoleLevel[]; } const INTEGRATION_NAME = 'ConsoleLogs' as const; const DEFAULT_ATTRIBUTES = { [SENTRY_ORIGIN]: 'auto.log.console', }; const _consoleLoggingIntegration = ((options: Partial = {}) => { const levels = options.levels || CONSOLE_LEVELS; return { name: INTEGRATION_NAME, setup(client) { const { normalizeDepth = 3, normalizeMaxBreadth = 1_000 } = client.getOptions(); const unsubscribe = addConsoleInstrumentationHandler(({ args, level }) => { if (getClient() !== client || !levels.includes(level)) { return; } const firstArg = args[0]; const followingArgs = args.slice(1); if (level === 'assert') { if (!firstArg) { const assertionMessage = followingArgs.length > 0 ? `Assertion failed: ${formatConsoleArgs(followingArgs, normalizeDepth, normalizeMaxBreadth)}` : 'Assertion failed'; captureLogEvent({ level: 'error', message: assertionMessage, attributes: DEFAULT_ATTRIBUTES }); } return; } const isLevelLog = level === 'log'; const attributes: Record = { ...DEFAULT_ATTRIBUTES }; if (isPlainObject(firstArg)) { // Object-first: extract object keys as attributes, remaining args as parameters Object.assign(attributes, normalize(firstArg, normalizeDepth, normalizeMaxBreadth)); const remainingArgsStartIndex = typeof args[1] === 'string' ? 2 : 1; const remainingArgs = args.slice(remainingArgsStartIndex); remainingArgs.forEach((arg, index) => { attributes[`sentry.message.parameter.${index}`] = normalize(arg, normalizeDepth, normalizeMaxBreadth); }); } else { // Fallback: template + parameters when first arg is a string without substitutions const shouldGenerateTemplate = followingArgs.length > 0 && typeof firstArg === 'string' && !hasConsoleSubstitutions(firstArg); if (shouldGenerateTemplate) { const templateAttrs = createConsoleTemplateAttributes(firstArg, followingArgs); for (const [key, value] of Object.entries(templateAttrs)) { attributes[key] = key.startsWith('sentry.message.parameter.') ? normalize(value, normalizeDepth, normalizeMaxBreadth) : value; } } } captureLogEvent({ level: isLevelLog ? 'info' : level, message: formatConsoleArgs(args, normalizeDepth, normalizeMaxBreadth), severityNumber: isLevelLog ? 10 : undefined, attributes, }); }); client.registerCleanup(unsubscribe); }, }; }) satisfies IntegrationFn; /** * Captures calls to the `console` API as logs in Sentry. * * @experimental This feature is experimental and may be changed or removed in future versions. * * By default the integration instruments `console.debug`, `console.info`, `console.warn`, `console.error`, * `console.log`, `console.trace`, and `console.assert`. You can use the `levels` option to customize which * levels are captured. * * @example * * ```ts * import * as Sentry from '@sentry/browser'; * * Sentry.init({ * integrations: [Sentry.consoleLoggingIntegration({ levels: ['error', 'warn'] })], * }); * ``` */ export const consoleLoggingIntegration = defineIntegration(_consoleLoggingIntegration);