MCPcopy Create free account
hub / github.com/getsentry/sentry-javascript / setup

Function setup

packages/core/src/logs/console-integration.ts:29–93  ·  view source on GitHub ↗
(client)

Source from the content-addressed store, hash-verified

27 return {
28 name: INTEGRATION_NAME,
29 setup(client) {
30 const { enableLogs, normalizeDepth = 3, normalizeMaxBreadth = 1_000 } = client.getOptions();
31 if (!enableLogs) {
32 DEBUG_BUILD && debug.warn('`enableLogs` is not enabled, ConsoleLogs integration disabled');
33 return;
34 }
35
36 const unsubscribe = addConsoleInstrumentationHandler(({ args, level }) => {
37 if (getClient() !== client || !levels.includes(level)) {
38 return;
39 }
40
41 const firstArg = args[0];
42 const followingArgs = args.slice(1);
43
44 if (level === 'assert') {
45 if (!firstArg) {
46 const assertionMessage =
47 followingArgs.length > 0
48 ? `Assertion failed: ${formatConsoleArgs(followingArgs, normalizeDepth, normalizeMaxBreadth)}`
49 : 'Assertion failed';
50 _INTERNAL_captureLog({ level: 'error', message: assertionMessage, attributes: DEFAULT_ATTRIBUTES });
51 }
52 return;
53 }
54
55 const isLevelLog = level === 'log';
56
57 const attributes: Record<string, unknown> = { ...DEFAULT_ATTRIBUTES };
58
59 if (isPlainObject(firstArg)) {
60 // Object-first: extract object keys as attributes, remaining args as parameters
61 Object.assign(attributes, normalize(firstArg, normalizeDepth, normalizeMaxBreadth));
62
63 const remainingArgsStartIndex = typeof args[1] === 'string' ? 2 : 1;
64 const remainingArgs = args.slice(remainingArgsStartIndex);
65
66 remainingArgs.forEach((arg, index) => {
67 attributes[`sentry.message.parameter.${index}`] = normalize(arg, normalizeDepth, normalizeMaxBreadth);
68 });
69 } else {
70 // Fallback: template + parameters when first arg is a string without substitutions
71 const shouldGenerateTemplate =
72 followingArgs.length > 0 && typeof firstArg === 'string' && !hasConsoleSubstitutions(firstArg);
73
74 if (shouldGenerateTemplate) {
75 const templateAttrs = createConsoleTemplateAttributes(firstArg, followingArgs);
76 for (const [key, value] of Object.entries(templateAttrs)) {
77 attributes[key] = key.startsWith('sentry.message.parameter.')
78 ? normalize(value, normalizeDepth, normalizeMaxBreadth)
79 : value;
80 }
81 }
82 }
83
84 _INTERNAL_captureLog({
85 level: isLevelLog ? 'info' : level,
86 message: formatConsoleArgs(args, normalizeDepth, normalizeMaxBreadth),

Callers

nothing calls this directly

Calls 15

getClientFunction · 0.90
formatConsoleArgsFunction · 0.90
_INTERNAL_captureLogFunction · 0.90
isPlainObjectFunction · 0.90
normalizeFunction · 0.90
hasConsoleSubstitutionsFunction · 0.90
sliceMethod · 0.80
assignMethod · 0.80
registerCleanupMethod · 0.80
getOptionsMethod · 0.65

Tested by

no test coverage detected