如何解决如何扩展可以接受args的console.log 更新:堆栈跟踪
我一直致力于扩展 console.log
以制作一个色彩丰富的保持堆栈跟踪它被调用的位置。我尝试了一些解决方案,但最终达到了这一点:
export const colorizedLog = (
text: string,status: keyof typeof ColorStatus = "dark",...args: any
) =>
console.log.bind(
console,`%c ${text}`,`font-weight:bold; color: ${ColorStatus[status]}`,...args
);
有了这个小binding
,我们将能够保留堆栈跟踪,但这里的问题是我们必须在最后使用一个恼人的额外()
,因为返回值是函数本身是 bind
的结果:
colorizedLog(
"Title:","info","This is a working example")();
我阅读过的其他主要资源如下:
const colorizedLog = (text,color= "#40a7e3",...args) =>
console.log.bind(
console,`font-weight:bold; color:${color}`,...args
);
colorizedLog("Title:","#40a7e3","This is a working example")();
更新:堆栈跟踪
解决方法
您可以立即调用它:
const colorizedLog = (text,color= "#40a7e3",...args) =>
console.log.bind(
console,`%c ${text}`,`font-weight:bold; color:${color}`,...args
)();
colorizedLog("Title:","#40a7e3","This is a working example");
,
如果您不介意添加另一个对该堆栈跟踪的调用,您可以将其包装在另一个隐藏额外 ()
的函数中:
const rawColorizedLog = (
text: string,status: keyof typeof ColorStatus = "dark",...args: any
) =>
console.log.bind(
console,`font-weight:bold; color: ${ColorStatus[status]}`,...args
);
export const colorizedLog = (...args: any) => rawColorizedLog(...args)();
然后你可以只调用 colorizedLog(...whatever)
但你会在堆栈中额外调用 rawColorizedLog
,但我相信你可以想出一个聪明的名字来覆盖 rawColorizedLog
用更酷的东西打电话给额外的电话;)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。