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