UNPKG

@sentry/utils

Version:
87 lines (74 loc) 2.4 kB
Object.defineProperty(exports, '__esModule', { value: true }); const worldwide = require('./worldwide.js'); /** Prefix for logging strings */ const PREFIX = 'Sentry Logger '; const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert', 'trace'] ; /** * Temporarily disable sentry console instrumentations. * * @param callback The function to run against the original `console` messages * @returns The results of the callback */ function consoleSandbox(callback) { if (!('console' in worldwide.GLOBAL_OBJ)) { return callback(); } const originalConsole = worldwide.GLOBAL_OBJ.console ; const wrappedLevels = {}; // Restore all wrapped console methods CONSOLE_LEVELS.forEach(level => { // TODO(v7): Remove this check as it's only needed for Node 6 const originalWrappedFunc = originalConsole[level] && (originalConsole[level] ).__sentry_original__; if (level in originalConsole && originalWrappedFunc) { wrappedLevels[level] = originalConsole[level] ; originalConsole[level] = originalWrappedFunc ; } }); try { return callback(); } finally { // Revert restoration to wrapped state Object.keys(wrappedLevels).forEach(level => { originalConsole[level] = wrappedLevels[level ]; }); } } function makeLogger() { let enabled = false; const logger = { enable: () => { enabled = true; }, disable: () => { enabled = false; }, }; if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { CONSOLE_LEVELS.forEach(name => { // eslint-disable-next-line @typescript-eslint/no-explicit-any logger[name] = (...args) => { if (enabled) { consoleSandbox(() => { worldwide.GLOBAL_OBJ.console[name](`${PREFIX}[${name}]:`, ...args); }); } }; }); } else { CONSOLE_LEVELS.forEach(name => { logger[name] = () => undefined; }); } return logger ; } // Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used exports.logger = void 0; if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { exports.logger = worldwide.getGlobalSingleton('logger', makeLogger); } else { exports.logger = makeLogger(); } exports.CONSOLE_LEVELS = CONSOLE_LEVELS; exports.consoleSandbox = consoleSandbox; //# sourceMappingURL=logger.js.map