logger.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const debugBuild = require('./debug-build.js');
  3. const worldwide = require('./worldwide.js');
  4. /** Prefix for logging strings */
  5. const PREFIX = 'Sentry Logger ';
  6. const CONSOLE_LEVELS = [
  7. 'debug',
  8. 'info',
  9. 'warn',
  10. 'error',
  11. 'log',
  12. 'assert',
  13. 'trace',
  14. ] ;
  15. /** This may be mutated by the console instrumentation. */
  16. const originalConsoleMethods
  17. = {};
  18. /** JSDoc */
  19. /**
  20. * Temporarily disable sentry console instrumentations.
  21. *
  22. * @param callback The function to run against the original `console` messages
  23. * @returns The results of the callback
  24. */
  25. function consoleSandbox(callback) {
  26. if (!('console' in worldwide.GLOBAL_OBJ)) {
  27. return callback();
  28. }
  29. const console = worldwide.GLOBAL_OBJ.console ;
  30. const wrappedFuncs = {};
  31. const wrappedLevels = Object.keys(originalConsoleMethods) ;
  32. // Restore all wrapped console methods
  33. wrappedLevels.forEach(level => {
  34. const originalConsoleMethod = originalConsoleMethods[level] ;
  35. wrappedFuncs[level] = console[level] ;
  36. console[level] = originalConsoleMethod;
  37. });
  38. try {
  39. return callback();
  40. } finally {
  41. // Revert restoration to wrapped state
  42. wrappedLevels.forEach(level => {
  43. console[level] = wrappedFuncs[level] ;
  44. });
  45. }
  46. }
  47. function makeLogger() {
  48. let enabled = false;
  49. const logger = {
  50. enable: () => {
  51. enabled = true;
  52. },
  53. disable: () => {
  54. enabled = false;
  55. },
  56. isEnabled: () => enabled,
  57. };
  58. if (debugBuild.DEBUG_BUILD) {
  59. CONSOLE_LEVELS.forEach(name => {
  60. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  61. logger[name] = (...args) => {
  62. if (enabled) {
  63. consoleSandbox(() => {
  64. worldwide.GLOBAL_OBJ.console[name](`${PREFIX}[${name}]:`, ...args);
  65. });
  66. }
  67. };
  68. });
  69. } else {
  70. CONSOLE_LEVELS.forEach(name => {
  71. logger[name] = () => undefined;
  72. });
  73. }
  74. return logger ;
  75. }
  76. const logger = makeLogger();
  77. exports.CONSOLE_LEVELS = CONSOLE_LEVELS;
  78. exports.consoleSandbox = consoleSandbox;
  79. exports.logger = logger;
  80. exports.originalConsoleMethods = originalConsoleMethods;
  81. //# sourceMappingURL=logger.js.map