handler.apply RangeError:超出最大调用堆栈大小
我试图理解为什么出现“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论