我基本上希望能够“打开调试日志”,就像在shell中运行的许多应用程序一样。实现它的天真方法是在我的每个函数中插入这样的语句:
if (DEBUG) console.log('Executing <name of function>');但是在每个函数中都有这样的功能似乎有点烦人。是否有一种更可靠或更清洁的方法来做到这一点?
发布于 2020-09-17 07:30:47
可以在日志语句中指定级别,如下所示
console.debug('this is a debug level log statement');
console.info('this is an info level log statement');
console.warn('this is a warn level log statement');在控制台查看器(例如Chrome中的开发工具)中,您可以选择要查看的日志消息。
发布于 2020-09-17 07:33:20
我现在处理它的方式是让我的记录器注入一个单例/依赖项对象,这样我就只需要编写
logger.debug('Something happening')其中显然包含
function debug(msg){ if (this.DEBUG) console.log(msg) }然后,我只需初始化它与适当的日志级别,我想在加载它。
https://softwareengineering.stackexchange.com/questions/415965
复制相似问题