|
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 | + // TODO(v7): Remove this check as it's only needed for Node 6 |
| 42 | + const originalWrappedFunc = |
| 43 | + originalConsole[level] && (originalConsole[level] as WrappedFunction).__sentry_original__; |
| 44 | + if (level in global.console && originalWrappedFunc) { |
| 45 | + wrappedLevels[level] = originalConsole[level] as LoggerConsoleMethods[typeof level]; |
| 46 | + originalConsole[level] = originalWrappedFunc as Console[typeof level]; |
44 | 47 | }
|
45 | 48 | });
|
46 | 49 |
|
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); |
| 50 | + try { |
| 51 | + return callback(); |
| 52 | + } finally { |
| 53 | + // Revert restoration to wrapped state |
| 54 | + Object.keys(wrappedLevels).forEach(level => { |
| 55 | + originalConsole[level] = wrappedLevels[level as typeof CONSOLE_LEVELS[number]]; |
85 | 56 | });
|
86 | 57 | }
|
| 58 | +} |
87 | 59 |
|
88 |
| - /** JSDoc */ |
89 |
| - public warn(...args: any[]): void { |
90 |
| - if (!this._enabled) { |
91 |
| - return; |
92 |
| - } |
93 |
| - consoleSandbox(() => { |
94 |
| - global.console.warn(`${PREFIX}[Warn]:`, ...args); |
| 60 | +function makeLogger(): Logger { |
| 61 | + let enabled = false; |
| 62 | + const logger: Partial<Logger> = { |
| 63 | + enable: () => { |
| 64 | + enabled = true; |
| 65 | + }, |
| 66 | + disable: () => { |
| 67 | + enabled = false; |
| 68 | + }, |
| 69 | + }; |
| 70 | + |
| 71 | + if (IS_DEBUG_BUILD) { |
| 72 | + CONSOLE_LEVELS.forEach(name => { |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 74 | + logger[name] = (...args: any[]) => { |
| 75 | + if (enabled) { |
| 76 | + consoleSandbox(() => { |
| 77 | + global.console[name](`${PREFIX}[${name}]:`, ...args); |
| 78 | + }); |
| 79 | + } |
| 80 | + }; |
95 | 81 | });
|
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); |
| 82 | + } else { |
| 83 | + CONSOLE_LEVELS.forEach(name => { |
| 84 | + logger[name] = () => undefined; |
105 | 85 | });
|
106 | 86 | }
|
107 |
| -} |
108 | 87 |
|
109 |
| -const sentryGlobal = global.__SENTRY__ || {}; |
110 |
| -const logger = (sentryGlobal.logger as Logger) || new Logger(); |
| 88 | + return logger as Logger; |
| 89 | +} |
111 | 90 |
|
| 91 | +// Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used |
| 92 | +let logger: Logger; |
112 | 93 | 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; |
| 94 | + logger = getGlobalSingleton('logger', makeLogger); |
| 95 | +} else { |
| 96 | + logger = makeLogger(); |
116 | 97 | }
|
117 | 98 |
|
118 | 99 | export { logger };
|
0 commit comments