@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
1 lines • 8.07 kB
Source Map (JSON)
{"version":3,"file":"logger.js","sources":["../../../src/utils/logger.ts"],"sourcesContent":["import { getGlobalSingleton } from '../carrier';\nimport { DEBUG_BUILD } from '../debug-build';\nimport type { ConsoleLevel } from '../types-hoist/instrument';\nimport { GLOBAL_OBJ } from './worldwide';\n\n/** A Sentry Logger instance. */\nexport interface Logger {\n disable(): void;\n enable(): void;\n isEnabled(): boolean;\n log(...args: Parameters<typeof console.log>): void;\n info(...args: Parameters<typeof console.info>): void;\n warn(...args: Parameters<typeof console.warn>): void;\n error(...args: Parameters<typeof console.error>): void;\n debug(...args: Parameters<typeof console.debug>): void;\n assert(...args: Parameters<typeof console.assert>): void;\n trace(...args: Parameters<typeof console.trace>): void;\n}\n\nexport interface SentryDebugLogger {\n disable(): void;\n enable(): void;\n isEnabled(): boolean;\n log(...args: Parameters<typeof console.log>): void;\n warn(...args: Parameters<typeof console.warn>): void;\n error(...args: Parameters<typeof console.error>): void;\n}\n\nexport const CONSOLE_LEVELS: readonly ConsoleLevel[] = [\n 'debug',\n 'info',\n 'warn',\n 'error',\n 'log',\n 'assert',\n 'trace',\n] as const;\n\n/** Prefix for logging strings */\nconst PREFIX = 'Sentry Logger ';\n\n/** This may be mutated by the console instrumentation. */\nexport const originalConsoleMethods: Partial<{\n log(...args: Parameters<typeof console.log>): void;\n info(...args: Parameters<typeof console.info>): void;\n warn(...args: Parameters<typeof console.warn>): void;\n error(...args: Parameters<typeof console.error>): void;\n debug(...args: Parameters<typeof console.debug>): void;\n assert(...args: Parameters<typeof console.assert>): void;\n trace(...args: Parameters<typeof console.trace>): void;\n}> = {};\n\n/**\n * Temporarily disable sentry console instrumentations.\n *\n * @param callback The function to run against the original `console` messages\n * @returns The results of the callback\n */\nexport function consoleSandbox<T>(callback: () => T): T {\n if (!('console' in GLOBAL_OBJ)) {\n return callback();\n }\n\n const console = GLOBAL_OBJ.console as Console;\n const wrappedFuncs: Partial<Record<ConsoleLevel, (...args: unknown[]) => void>> = {};\n\n const wrappedLevels = Object.keys(originalConsoleMethods) as ConsoleLevel[];\n\n // Restore all wrapped console methods\n wrappedLevels.forEach(level => {\n const originalConsoleMethod = originalConsoleMethods[level];\n wrappedFuncs[level] = console[level] as (...args: unknown[]) => void;\n console[level] = originalConsoleMethod as (...args: unknown[]) => void;\n });\n\n try {\n return callback();\n } finally {\n // Revert restoration to wrapped state\n wrappedLevels.forEach(level => {\n console[level] = wrappedFuncs[level] as (...args: unknown[]) => void;\n });\n }\n}\n\nfunction enable(): void {\n _getLoggerSettings().enabled = true;\n}\n\nfunction disable(): void {\n _getLoggerSettings().enabled = false;\n}\n\nfunction isEnabled(): boolean {\n return _getLoggerSettings().enabled;\n}\n\nfunction log(...args: Parameters<typeof console.log>): void {\n _maybeLog('log', ...args);\n}\n\nfunction info(...args: Parameters<typeof console.info>): void {\n _maybeLog('info', ...args);\n}\n\nfunction warn(...args: Parameters<typeof console.warn>): void {\n _maybeLog('warn', ...args);\n}\n\nfunction error(...args: Parameters<typeof console.error>): void {\n _maybeLog('error', ...args);\n}\n\nfunction _debug(...args: Parameters<typeof console.debug>): void {\n _maybeLog('debug', ...args);\n}\n\nfunction assert(...args: Parameters<typeof console.assert>): void {\n _maybeLog('assert', ...args);\n}\n\nfunction trace(...args: Parameters<typeof console.trace>): void {\n _maybeLog('trace', ...args);\n}\n\nfunction _maybeLog(level: ConsoleLevel, ...args: Parameters<(typeof console)[typeof level]>): void {\n if (!DEBUG_BUILD) {\n return;\n }\n\n if (isEnabled()) {\n consoleSandbox(() => {\n GLOBAL_OBJ.console[level](`${PREFIX}[${level}]:`, ...args);\n });\n }\n}\n\nfunction _getLoggerSettings(): { enabled: boolean } {\n if (!DEBUG_BUILD) {\n return { enabled: false };\n }\n\n return getGlobalSingleton('loggerSettings', () => ({ enabled: false }));\n}\n\n/**\n * This is a logger singleton which either logs things or no-ops if logging is not enabled.\n * The logger is a singleton on the carrier, to ensure that a consistent logger is used throughout the SDK.\n */\nexport const logger = {\n /** Enable logging. */\n enable,\n /** Disable logging. */\n disable,\n /** Check if logging is enabled. */\n isEnabled,\n /** Log a message. */\n log,\n /** Log level info */\n info,\n /** Log a warning. */\n warn,\n /** Log an error. */\n error,\n /** Log a debug message. */\n debug: _debug,\n /** Log an assertion. */\n assert,\n /** Log a trace. */\n trace,\n} satisfies Logger;\n\n/**\n * This is a logger singleton which either logs things or no-ops if logging is not enabled.\n */\nexport const debug = {\n /** Enable logging. */\n enable,\n /** Disable logging. */\n disable,\n /** Check if logging is enabled. */\n isEnabled,\n /** Log a message. */\n log,\n /** Log a warning. */\n warn,\n /** Log an error. */\n error,\n} satisfies SentryDebugLogger;\n"],"names":["GLOBAL_OBJ","DEBUG_BUILD","getGlobalSingleton"],"mappings":";;;;;;AAKA;;AAuBO,MAAM,cAAc,GAA4B;AACvD,EAAE,OAAO;AACT,EAAE,MAAM;AACR,EAAE,MAAM;AACR,EAAE,OAAO;AACT,EAAE,KAAK;AACP,EAAE,QAAQ;AACV,EAAE,OAAO;AACT,CAAA;;AAEA;AACA,MAAM,MAAA,GAAS,gBAAgB;;AAE/B;MACa;;AAQb,GAAK;;AAEL;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,cAAc,CAAI,QAAQ,EAAc;AACxD,EAAE,IAAI,EAAE,aAAaA,oBAAU,CAAC,EAAE;AAClC,IAAI,OAAO,QAAQ,EAAE;AACrB;;AAEA,EAAE,MAAM,OAAA,GAAUA,oBAAU,CAAC,OAAA;AAC7B,EAAE,MAAM,YAAY,GAAgE,EAAE;;AAEtF,EAAE,MAAM,gBAAgB,MAAM,CAAC,IAAI,CAAC,sBAAsB,CAAA;;AAE1D;AACA,EAAE,aAAa,CAAC,OAAO,CAAC,SAAS;AACjC,IAAI,MAAM,qBAAA,GAAwB,sBAAsB,CAAC,KAAK,CAAC;AAC/D,IAAI,YAAY,CAAC,KAAK,CAAA,GAAI,OAAO,CAAC,KAAK,CAAA;AACvC,IA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