javascript - 如何扩展可以接受 args 的 console.log

标签 javascript typescript bind console.log javascript-debugger

我一直在努力扩展 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")();
我读过的其他主要资源如下:
  • Extending console.log
  • Macro using babel- kent.c dodds
  • Webpack Define plugin

  • 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")();

    更新:堆栈跟踪
    使用AppVersion.ts
    enter image description here
    应用程序.vue
    enter image description here
    Chrome 控制台
    enter image description here

    最佳答案

    您可以立即调用它:

    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");
    

    关于javascript - 如何扩展可以接受 args 的 console.log,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66182606/

    相关文章:

    javascript - 为什么这不会导致浏览器重定向?

    javascript - EMBER直接路由URL访问不加载数据

    html - PrimeNG p-checkbox - 在标签后添加星号强制符号

    Angular 2 : Binding dropdowns values with 'n' numbers

    sockets - 绑定(bind)多播 (UDP) 套接字意味着什么?

    javascript - REGEX:如何解析同一前缀文本内的可变数量的数据点

    javascript - 使用 javascript 在 IE8 和其他浏览器中获取主体类的标准解决方案是什么

    javascript - 使用 rxjs subject 在 Angular 7 中显示和隐藏组件

    c++ - std::bind 绑定(bind)函数

    javascript - 在 Javascript 中绑定(bind)闭包变量