handler.apply RangeError:超出最大调用堆栈大小

发布于 2025-01-11 05:51:30 字数 1677 浏览 0 评论 0原文

我试图理解为什么出现“RangeError:超出最大调用堆栈大小”错误。我们有一个需要使用的记录器。我想做的,每当你调用记录器 fe logger.debug('My message') 我想注入额外的字段,在我的例子中它是线程 id、相关 id 等。我所做的是

// 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 };

它完美地工作。但是当我运行测试时,即使是一个文件,我也会收到此

    RangeError: Maximum call stack size exceeded

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

错误我认为我可能是由于 if (prop === 'createLogger') 行,但在该测试中我没有创建新的记录器,只是从该文件导入它并使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文