console.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { CONSOLE_LEVELS, originalConsoleMethods } from '../logger.js';
  2. import { fill } from '../object.js';
  3. import { GLOBAL_OBJ } from '../worldwide.js';
  4. import { addHandler, maybeInstrument, triggerHandlers } from './_handlers.js';
  5. /**
  6. * Add an instrumentation handler for when a console.xxx method is called.
  7. *
  8. * Use at your own risk, this might break without changelog notice, only used internally.
  9. * @hidden
  10. */
  11. function addConsoleInstrumentationHandler(handler) {
  12. const type = 'console';
  13. addHandler(type, handler);
  14. maybeInstrument(type, instrumentConsole);
  15. }
  16. function instrumentConsole() {
  17. if (!('console' in GLOBAL_OBJ)) {
  18. return;
  19. }
  20. CONSOLE_LEVELS.forEach(function (level) {
  21. if (!(level in GLOBAL_OBJ.console)) {
  22. return;
  23. }
  24. fill(GLOBAL_OBJ.console, level, function (originalConsoleMethod) {
  25. originalConsoleMethods[level] = originalConsoleMethod;
  26. return function (...args) {
  27. const handlerData = { args, level };
  28. triggerHandlers('console', handlerData);
  29. const log = originalConsoleMethods[level];
  30. log && log.apply(GLOBAL_OBJ.console, args);
  31. };
  32. });
  33. });
  34. }
  35. export { addConsoleInstrumentationHandler };
  36. //# sourceMappingURL=console.js.map