'handler.apply RangeError: Maximum call stack size exceeded

I'm trying to understand why there's "RangeError: Maximum call stack size exceeded" error. We have a logger that we need to use. What I want to do, anytime you call the logger f.e logger.debug('My message') I want to inject additional fields, in my case it's thread id, correlation id etc. What I did is

// Main logger
const Log = require('ethos-js-logger');

const logLevels = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
const ethosLogger = Log.createLogger();

const handler = {
  get(target: any, prop: string) {
    // Adding additional information into logger
    if (logLevels.includes(prop)) {
      const correlationId = httpContext.get('correlationId');
      const endpoint = httpContext.get('endpoint');
      const apiKey = httpContext.get('apiKey');

      const message = {
        correlationId,
        endpoint,
        apiKey,
        isMainThread,
        ...(!isMainThread && { WorkerThreadId: threadId }),
      };
      return (args: any) => {
        target[prop].apply(target, [args, message]);
      };
    }

    // When create a new instance of logger return Proxy
    if (prop === 'createLogger') {
      return (args: any) => {
        return new Proxy(Log.createLogger(args), handler);
      };
    }

    // Return default logger implementation
    return target[prop];
  },
};

const logger = new Proxy(ethosLogger, handler);

export { logger };

It perfectly works. But when I run tests, even one file, I'm getting this error

    RangeError: Maximum call stack size exceeded

      24 |       };
      25 |       return (args: any) => {
    > 26 |         target[prop].apply(target, [args, message]);
         |                            ^
      27 |       };
      28 |     }

P.S. I thought I could be due to if (prop === 'createLogger') line, but in that test I don't create a new logger, just import it from this file and use.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source