|
| 1 | +import { addBreadcrumb } from '../breadcrumbs'; |
| 2 | +import { getClient } from '../currentScopes'; |
| 3 | +import { defineIntegration } from '../integration'; |
| 4 | +import type { ConsoleLevel } from '../types-hoist'; |
| 5 | +import { |
| 6 | + CONSOLE_LEVELS, |
| 7 | + GLOBAL_OBJ, |
| 8 | + addConsoleInstrumentationHandler, |
| 9 | + safeJoin, |
| 10 | + severityLevelFromString, |
| 11 | +} from '../utils-hoist'; |
| 12 | + |
| 13 | +interface ConsoleIntegrationOptions { |
| 14 | + levels: ConsoleLevel[]; |
| 15 | +} |
| 16 | + |
| 17 | +type GlobalObjectWithUtil = typeof GLOBAL_OBJ & { |
| 18 | + util: { |
| 19 | + format: (...args: unknown[]) => string; |
| 20 | + }; |
| 21 | +}; |
| 22 | + |
| 23 | +const INTEGRATION_NAME = 'Console'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Captures calls to the `console` API as breadcrumbs in Sentry. |
| 27 | + * |
| 28 | + * By default the integration instruments `console.debug`, `console.info`, `console.warn`, `console.error`, |
| 29 | + * `console.log`, `console.trace`, and `console.assert`. You can use the `levels` option to customize which |
| 30 | + * levels are captured. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * |
| 34 | + * ```js |
| 35 | + * Sentry.init({ |
| 36 | + * integrations: [Sentry.consoleIntegration({ levels: ['error', 'warn'] })], |
| 37 | + * }); |
| 38 | + * ``` |
| 39 | + */ |
| 40 | +export const consoleIntegration = defineIntegration((options: Partial<ConsoleIntegrationOptions> = {}) => { |
| 41 | + const levels = new Set(options.levels || CONSOLE_LEVELS); |
| 42 | + |
| 43 | + return { |
| 44 | + name: INTEGRATION_NAME, |
| 45 | + setup(client) { |
| 46 | + addConsoleInstrumentationHandler(({ args, level }) => { |
| 47 | + if (getClient() !== client || !levels.has(level)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + addConsoleBreadcrumb(level, args); |
| 52 | + }); |
| 53 | + }, |
| 54 | + }; |
| 55 | +}); |
| 56 | + |
| 57 | +/** |
| 58 | + * Capture a console breadcrumb. |
| 59 | + * |
| 60 | + * Exported just for tests. |
| 61 | + */ |
| 62 | +export function addConsoleBreadcrumb(level: ConsoleLevel, args: unknown[]): void { |
| 63 | + const breadcrumb = { |
| 64 | + category: 'console', |
| 65 | + data: { |
| 66 | + arguments: args, |
| 67 | + logger: 'console', |
| 68 | + }, |
| 69 | + level: severityLevelFromString(level), |
| 70 | + message: formatConsoleArgs(args), |
| 71 | + }; |
| 72 | + |
| 73 | + if (level === 'assert') { |
| 74 | + if (args[0] === false) { |
| 75 | + const assertionArgs = args.slice(1); |
| 76 | + breadcrumb.message = |
| 77 | + assertionArgs.length > 0 ? `Assertion failed: ${formatConsoleArgs(assertionArgs)}` : 'Assertion failed'; |
| 78 | + breadcrumb.data.arguments = assertionArgs; |
| 79 | + } else { |
| 80 | + // Don't capture a breadcrumb for passed assertions |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + addBreadcrumb(breadcrumb, { |
| 86 | + input: args, |
| 87 | + level, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function formatConsoleArgs(values: unknown[]): string { |
| 92 | + return 'util' in GLOBAL_OBJ && typeof (GLOBAL_OBJ as GlobalObjectWithUtil).util.format === 'function' |
| 93 | + ? (GLOBAL_OBJ as GlobalObjectWithUtil).util.format(...values) |
| 94 | + : safeJoin(values, ' '); |
| 95 | +} |
0 commit comments