|
1 | | -/* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | 1 | import { WrappedFunction } from '@sentry/types'; |
3 | 2 |
|
4 | 3 | import { IS_DEBUG_BUILD } from './flags'; |
5 | | -import { getGlobalObject } from './global'; |
| 4 | +import { getGlobalObject, getGlobalSingleton } from './global'; |
6 | 5 |
|
7 | 6 | // TODO: Implement different loggers for different environments |
8 | 7 | const global = getGlobalObject<Window | NodeJS.Global>(); |
9 | 8 |
|
10 | 9 | /** Prefix for logging strings */ |
11 | 10 | const PREFIX = 'Sentry Logger '; |
12 | 11 |
|
13 | | -export const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert']; |
| 12 | +export const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert'] as const; |
| 13 | + |
| 14 | +type LoggerMethod = (...args: unknown[]) => void; |
| 15 | +type LoggerConsoleMethods = Record<typeof CONSOLE_LEVELS[number], LoggerMethod>; |
14 | 16 |
|
15 | 17 | /** JSDoc */ |
16 | | -interface ExtensibleConsole extends Console { |
17 | | - [key: string]: any; |
| 18 | +interface Logger extends LoggerConsoleMethods { |
| 19 | + disable(): void; |
| 20 | + enable(): void; |
18 | 21 | } |
19 | 22 |
|
20 | 23 | /** |
21 | | - * Temporarily unwrap `console.log` and friends in order to perform the given callback using the original methods. |
22 | | - * Restores wrapping after the callback completes. |
| 24 | + * Temporarily disable sentry console instrumentations. |
23 | 25 | * |
24 | 26 | * @param callback The function to run against the original `console` messages |
25 | 27 | * @returns The results of the callback |
26 | 28 | */ |
27 | | -export function consoleSandbox(callback: () => any): any { |
| 29 | +export function consoleSandbox<T>(callback: () => T): T { |
28 | 30 | const global = getGlobalObject<Window>(); |
29 | 31 |
|
30 | 32 | if (!('console' in global)) { |
31 | 33 | return callback(); |
32 | 34 | } |
33 | 35 |
|
34 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
35 | | - const originalConsole = (global as any).console as ExtensibleConsole; |
36 | | - const wrappedLevels: { [key: string]: any } = {}; |
| 36 | + const originalConsole = global.console as Console & Record<string, unknown>; |
| 37 | + const wrappedLevels: Partial<LoggerConsoleMethods> = {}; |
37 | 38 |
|
38 | 39 | // Restore all wrapped console methods |
39 | 40 | CONSOLE_LEVELS.forEach(level => { |
40 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
41 | | - if (level in (global as any).console && (originalConsole[level] as WrappedFunction).__sentry_original__) { |
42 | | - wrappedLevels[level] = originalConsole[level] as WrappedFunction; |
43 | | - originalConsole[level] = (originalConsole[level] as WrappedFunction).__sentry_original__; |
| 41 | + const originalWrappedFunc = (originalConsole[level] as WrappedFunction).__sentry_original__; |
| 42 | + if (level in global.console && originalWrappedFunc) { |
| 43 | + wrappedLevels[level] = originalConsole[level] as LoggerConsoleMethods[typeof level]; |
| 44 | + originalConsole[level] = originalWrappedFunc as Console[typeof level]; |
44 | 45 | } |
45 | 46 | }); |
46 | 47 |
|
47 | | - // Perform callback manipulations |
48 | | - const result = callback(); |
49 | | - |
50 | | - // Revert restoration to wrapped state |
51 | | - Object.keys(wrappedLevels).forEach(level => { |
52 | | - originalConsole[level] = wrappedLevels[level]; |
53 | | - }); |
54 | | - |
55 | | - return result; |
56 | | -} |
57 | | - |
58 | | -/** JSDoc */ |
59 | | -class Logger { |
60 | | - /** JSDoc */ |
61 | | - private _enabled: boolean; |
62 | | - |
63 | | - /** JSDoc */ |
64 | | - public constructor() { |
65 | | - this._enabled = false; |
66 | | - } |
67 | | - |
68 | | - /** JSDoc */ |
69 | | - public disable(): void { |
70 | | - this._enabled = false; |
71 | | - } |
72 | | - |
73 | | - /** JSDoc */ |
74 | | - public enable(): void { |
75 | | - this._enabled = true; |
76 | | - } |
77 | | - |
78 | | - /** JSDoc */ |
79 | | - public log(...args: any[]): void { |
80 | | - if (!this._enabled) { |
81 | | - return; |
82 | | - } |
83 | | - consoleSandbox(() => { |
84 | | - global.console.log(`${PREFIX}[Log]:`, ...args); |
| 48 | + try { |
| 49 | + return callback(); |
| 50 | + } finally { |
| 51 | + // Revert restoration to wrapped state |
| 52 | + Object.keys(wrappedLevels).forEach(level => { |
| 53 | + originalConsole[level] = wrappedLevels[level as typeof CONSOLE_LEVELS[number]]; |
85 | 54 | }); |
86 | 55 | } |
| 56 | +} |
87 | 57 |
|
88 | | - /** JSDoc */ |
89 | | - public warn(...args: any[]): void { |
90 | | - if (!this._enabled) { |
91 | | - return; |
92 | | - } |
93 | | - consoleSandbox(() => { |
94 | | - global.console.warn(`${PREFIX}[Warn]:`, ...args); |
| 58 | +function makeLogger(): Logger { |
| 59 | + let enabled = false; |
| 60 | + const logger: Partial<Logger> = { |
| 61 | + enable: () => { |
| 62 | + enabled = true; |
| 63 | + }, |
| 64 | + disable: () => { |
| 65 | + enabled = false; |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + if (IS_DEBUG_BUILD) { |
| 70 | + CONSOLE_LEVELS.forEach(name => { |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 72 | + logger[name] = (...args: any[]) => { |
| 73 | + if (enabled) { |
| 74 | + consoleSandbox(() => { |
| 75 | + global.console[name](`${PREFIX}[${name}]:`, ...args); |
| 76 | + }); |
| 77 | + } |
| 78 | + }; |
95 | 79 | }); |
96 | | - } |
97 | | - |
98 | | - /** JSDoc */ |
99 | | - public error(...args: any[]): void { |
100 | | - if (!this._enabled) { |
101 | | - return; |
102 | | - } |
103 | | - consoleSandbox(() => { |
104 | | - global.console.error(`${PREFIX}[Error]:`, ...args); |
| 80 | + } else { |
| 81 | + CONSOLE_LEVELS.forEach(name => { |
| 82 | + logger[name] = () => undefined; |
105 | 83 | }); |
106 | 84 | } |
107 | | -} |
108 | 85 |
|
109 | | -const sentryGlobal = global.__SENTRY__ || {}; |
110 | | -const logger = (sentryGlobal.logger as Logger) || new Logger(); |
| 86 | + return logger as Logger; |
| 87 | +} |
111 | 88 |
|
| 89 | +// Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used |
| 90 | +let logger: Logger; |
112 | 91 | if (IS_DEBUG_BUILD) { |
113 | | - // Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used |
114 | | - sentryGlobal.logger = logger; |
115 | | - global.__SENTRY__ = sentryGlobal; |
| 92 | + logger = getGlobalSingleton('logger', makeLogger); |
| 93 | +} else { |
| 94 | + logger = makeLogger(); |
116 | 95 | } |
117 | 96 |
|
118 | 97 | export { logger }; |
0 commit comments