vue中重写并自定义console.log

news2024/9/28 17:36:02

0. 背景

vue2项目中自定义console.log并输出文件名及行、列号

1. 实现

1.1 自定义console.log

在这里插入图片描述

export default {
  // 输出等级: 0-no, 1-error, 2-warning, 3-info, 4-debug, 5-log
  level: 5,
  // 输出模式: 0-default, 1-normal, 2-random
  mode: 1,
  // 是否输出图标
  hasIcon: false,
  // 是否在vue内使用
  isVue: true,
  // 是否打印函数名和所在文件行号
  isPrintLine: true,
  // 图标
  icons: ['🌵', '🎍', '🐾', '🌀', '🐚', '🥝', '🥜', '🥕', '🥒', '🌽', '🍒', '🍅', '🍑', '🍋', '🍈', '🌶', '🌰', '🍠', '🍆', '🍄', '🍐', '🍌', '🍍', '🍇', '🍏', '🍓', '🍎', '🍊', '🐴', '🐗', '🦄', '🐑', '🐶', '🐔', '🐼', '🐒', '🌝', '💄', '💋', '👠', '👗', '👙', '🧣', '🍰', '🍭', '🍳', '🎄', '🎱', '⚽', '🏀', '🎵', '🚄', '⭕', '❌', '❓', '❗', '💯'],
  // 标准颜色
  colors: {
    error: '#f7630c',
    warning: '#ca5010',
    info: '#0078d7',
    debug: '#13a10e',
    log: '#1f1f1f'
  },
  // 获取随机图标
  randomIcon: function () {
    return this.icons[Math.floor(Math.random() * this.icons.length)]
  },
  // 获取随机颜色
  randomColor: function () {
    const r = Math.floor(Math.random() * 256)
    const g = Math.floor(Math.random() * 256)
    const b = Math.floor(Math.random() * 256)
    // 返回随机生成的颜色
    return `rgb(${r}, ${g}, ${b})`
  },
  // 默认打印
  printDefault: function (tag, args) {
    console.log(tag, ...args)
  },
  // 标准打印
  printNormal: function (tag, args) {
    console.log(`%c ${tag} : `, `color: ${this.colors[tag]}`, ...args)
  },
  // 随机打印
  printRandom: function (tag, args) {
    const icon = this.randomIcon()
    const bgColor = this.randomColor()
    const color = this.randomColor()
    console.log(`%c ${icon}`, `font-size:20px;background-color: ${bgColor};color: ${color};`, tag + ' : ', ...args)
  },
  print: function (tag, args) {
    if (this.isPrintLine) {
      if (!this.isVue) {
        // 获取函数名和行号
        const err = new Error()
        // console.log(err.stack)
        const stack = err.stack.split('\n').slice(3).map(line => line.trim())
        // console.log(stack)
        const caller = stack[0].match(/at (.+) \(/) ? stack[0].match(/at (.+) \(/)[1] : stack[0].match(/at (.+):\d+:/)[1]
        const fileLine = stack[0].match(/\(.*\/(.+)\)/) ? stack[0].match(/\(.*\/(.+)\)/)[1] : stack[0].match(/(\d+:\d+)/)[1]
        // console.log(`${caller} (${fileLine}):\n`)

        args.shift(`[${caller} (${fileLine})]\n`)
      } else {
        args.shift()
      }
    } else {
      if (this.isVue) {
        args.shift().shift()
      }
    }

    switch (this.mode) {
      case 0: {
        this.printDefault(tag, args)
        break
      }
      case 1: {
        this.printNormal(tag, args)
        break
      }
      case 2: {
        this.printRandom(tag, args)
        break
      }
    }
  },
  error: (function (oriLogFunc) {
    return function (...args) {
      const tag = 'error'
      if (this.level >= 1) {
        // oriLogFunc.call(console, 'error : ', args)
        this.print(tag, args)
      }
    }
  })(console.log),

  warning: (function (oriLogFunc) {
    return function (...args) {
      const tag = 'warning'
      if (this.level >= 2) {
        // oriLogFunc.call(console, 'warning : ', args)
        this.print(tag, args)
      }
    }
  })(console.log),

  info: (function (oriLogFunc) {
    return function (...args) {
      const tag = 'info'
      if (this.level >= 3) {
        // oriLogFunc.call(console, 'info : ', args)
        this.print(tag, args)
      }
    }
  })(console.log),

  debug: (function (oriLogFunc) {
    return function (...args) {
      const tag = 'debug'
      if (this.level >= 4) {
        // oriLogFunc.call(console, 'debug : ', ...args)
        this.print(tag, args)
      }
    }
  })(console.log),

  log: (function (oriLogFunc) {
    return function (...args) {
      const tag = 'log'
      if (this.level >= 5) {
        // oriLogFunc.call(console, 'log : ', ...args)
        this.print(tag, args)
      }
    }
  })(console.log)
}

1.2 webpack记录行号

添加自定义loader

在这里插入图片描述

module.exports = function (content) {
  content = content.toString('utf-8')
  if (this.cacheable) this.cacheable()
  const { name = ['this.\\$iceLog.log'] } = this.query.config || {}
  const fileName = this.resourcePath.replaceAll('\\', '/').match(/(?<=\/)(src.*)/gm)[0]
  content = content
    .split('\n')
    .map((line, row) => {
      let loggerName = name[0]
      for (let i = 1; i < name.length; i++) {
        loggerName += '|' + name[i]
      }
      const re = new RegExp(`(${loggerName})\\((.*?)\\)`, 'g')
      let result
      let newLine = ''
      let cursor = 0
      while ((result = re.exec(line))) {
        const col = result.index
        newLine += line.slice(cursor, result.index) + `${result[1]}('[${fileName}:${row + 1}:${col + 1}]\\n', ` + result[2] + ')'
        cursor += col + result[0].length
      }
      newLine += line.slice(cursor)
      return newLine
    })
    .join('\n')

  return content
}
module.exports.raw = true

1.3 配置loader

修改vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  chainWebpack: config => {
    // config.devtool('eval-cheap-module-source-map')
    config.module
      .rule('vue')
      .use('vue-loader')
      .end()
      .rule('log-lineno')
      .use('./loaders/log-lineno.loader')
      .loader('./loaders/log-lineno.loader')
      .options({
        config: {
          name: [
            'this.\\$iceLog.error',
            'this.\\$iceLog.warning',
            'this.\\$iceLog.info',
            'this.\\$iceLog.debug',
            'this.\\$iceLog.log'
          ]
        }
      })
      .end()
  }
})

2. 测试

  created () {
    this.$iceLog.log(123)
    this.$iceLog.error(1, 2, 3, 4)
  },

在这里插入图片描述

3. 问题

3.1 .vue文件被编译多次,自定义log会输出两个目录

在这里插入图片描述
解决:
在自定义函数中移除一个参数:

在这里插入图片描述

x. 参考

  1. 重写并自定义console.log()输出样式
  2. 巧用 webpack 在日志中记录文件行号
  3. vue webpace相关
  4. Vue2工程化 webpack配置 loader插件
  5. webpack初学者看这篇就够了
  6. 你了解webpack中配置的loader的执行顺序吗?

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/637056.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数组判断某个属性是否都相同

一、 // 判断属性是否存在isPropertyAllSame(array, property) {if (array.length 0) {return true; // 空数组默认属性全部相同}const firstPropertyValue array[0][property]; // 取第一个元素的属性值for (let i 1; i < array.length; i) {if (array[i][property] ! f…

5.2.10 IP分组的转发(一)

5.2.10 IP分组的转发&#xff08;一&#xff09; 我们已经知道对于IP协议来说提供的是无连接、不可靠、尽力而为的IP分组交付服务&#xff0c;这里我们就学习一下一个IP分组是如何从源主机交付给目的主机的。如果在因特网上有两台主机发送数据的时候&#xff0c;分组究竟是如何…

轻松下载google drive大文件 IDM微操教程

背景 在google drive使用chrome浏览器自带的下载工具&#xff0c;下载时总是报错&#xff1a; 于是在网上搜索"下载google drive 大文件"&#xff0c;看到有人提到了IDM和gdown。最终用IDM解决了需求。从下图可见&#xff0c;文件有99GB&#xff0c;每秒下载速度10…

媒体分类详解,企业做活动可以邀请哪些媒体?

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 媒体分类可以根据不同的维度进行划分。以下是一些常见的媒体分类方式&#xff1a; 1. 传统媒体&#xff1a; - 报纸&#xff1a;报纸是最传统的媒体形式之一&#xff0c;以印刷纸质媒体为…

华为OD机试真题 JavaScript 实现【DNA序列】【牛客练习题】

一、题目描述 一个 DNA 序列由 A/C/G/T 四个字母的排列组合组成。 G 和 C 的比例&#xff08;定义为 GC-Ratio &#xff09;是序列中 G 和 C 两个字母的总的出现次数除以总的字母数目&#xff08;也就是序列长度&#xff09;。在基因工程中&#xff0c;这个比例非常重要。因为…

SpringBoot:SpringBoot启动加载过程 ④

一、思想 我们看到技术上高效简单的使用&#xff0c;其实背后除了奇思妙想的开创性设计&#xff0c;另一点是别人帮你做了复杂繁琐的事情。 二、从官网Demo入手 官网就一行代码。这个就是它的启动代码。 1、SpringBootApplication注解 ①. 三个核心注解的整合。 SpringBootCon…

什么是微带线和带状线

在电路板设计中&#xff0c;微带线和带状线分别是用于传输信号的两种常见的传输线路。虽然在许多方面它们很相似&#xff0c;但是它们的物理结构、传输速率、特性阻抗等方面存在很大的差异。 本文将介绍微带线和带状线的基本概念、特性和应用场景&#xff0c;并比较它们的优缺…

Snap版本的CUPS将在Ubuntu 23.10 中默认使用

导读CUPS 是一个用于 Linux 系统的开源模块化打印系统&#xff0c;最初由 Michael Sweet 开发。在 2007 年&#xff0c;苹果公司收购了 CUPS&#xff0c;并聘请了 Michael Sweet 继续开发。 CUPS 是一个用于 Linux 系统的开源模块化打印系统&#xff0c;最初由 Michael Sweet …

搭建一个免费代理IP池,妈妈再也不用担心我没有IP用了

背景 爬虫工作才刚刚开始&#xff0c;突然IP被限制了&#xff0c;怎么办&#xff1f;他们告诉我&#xff0c;可以去买代理IP呀。 可是我翻了5遍裤兜&#xff0c;查了8张银行卡&#xff0c;却只有1块钱 &#xff0c;根本买不起啊。 突然&#xff0c;隔壁部门的同事的姐姐的妈妈…

k8s pod state terminating学习笔记

文章目录 背景解决方式个人思考 背景 今天服务器重启失败&#xff0c;于是去检查k8s状态&#xff0c;发现pod一直处于terminating状态。 多次尝试删除pod但是失败了&#xff0c;于是想到可能是k8s node卡住了导致接收不到kill命令。 解决方式 所以reboot k8s node&#xff0…

华夏erp账号任意用户登录

hunter&#xff1a; web.icon"f6efcd53ba2b07d67ab993073c238a11" 访问/jshERP-boot/user/getAllList;.ico接口可查看所有用户账号密码(密码为md5值) GET /jshERP-boot/user/getAllList;.ico HTTP/1.1 Host: xxx User-Agent: Mozilla/5.0 (Windows NT 10.0; Win…

加密与解密 调试篇 动态调试技术 (四)-x64dbg/MDebug

x64dbg是开源的调试器 支持 32位和64位 Download x64dbg 我们使用64位程序进行实验 加载TraceMe64 然后我们通过之前了解到了 TraceMe是用 GetDlgItemTextA来读取我们输入的值 所以我们在x64dbg中对其进行断点 但是我们先要设置 x64dbg在加载程序的时候是在系统断点处所…

本原多项式和不可约多项式

本文讨论的&#xff0c;不做特别说明的话&#xff0c;都是在伽罗华域(Galois Fields)上。 1、不可约多项式&#xff08;Irreducible Polynomial&#xff09; 定义&#xff1a;不能写成两个次数较低的多项式乘积形式的多项式。 多项式分解网站&#xff1a;EE4253 Polynomial …

中尺度气象学期末复习笔记

中尺度气象学期末复习笔记 文章目录 中尺度气象学期末复习笔记三个表示天气的词之间的关系&#xff08;一&#xff09;天气系统的尺度划分1.1 为什么要进行尺度分析1.2 经验分类Ligda对中尺度的定义 1.3 理论分类谱分析通过无量纲数进行分类 1.4 实用分类奥兰斯基分类AEIOU分类…

如何恢复u盘数据?数据恢复,就看这4个方法!

案例&#xff1a;每次清理u盘时&#xff0c;总是会不小心删除重要的数据&#xff0c;如何恢复u盘数据呢&#xff1f; 【u盘中的数据删除了还能恢复吗&#xff1f;我昨天想把u盘里一些不需要的图片和文件删除&#xff0c;但不小心删了些有用的数据&#xff0c;有什么方法可以帮我…

【洛谷】P8306 【模板】字典树

&#xff08;最后有解释哦&#xff09; 0:所需参数 const int N3e610;int t[N][70],cnt[N],idx; char s[N]; 1.映射字符 int getnum(char x) {if(x>A&&x<Z) return x-A;else if(x>a&&x<z) return x-a26;else return x-052; } 2.插入字符串 voi…

pdf文档怎么转换成excel?分享这几个方法!

当涉及将PDF转换为Excel时&#xff0c;许多人可能会遇到困扰。PDF因其统一性、稳定性和不可编辑性而被广泛应用于日常工作和学习中。然而&#xff0c;在某些特殊情况下&#xff0c;例如在网络上找到的PDF资料中需要借鉴其中的内容制作Excel文档&#xff0c;我们需要找到方法将P…

谷歌Imagen Editor融入AI技术,助力图片创作

AI科技的应用越来越广泛&#xff0c;自然也被各个公司所重视&#xff0c;近日谷歌就推出利用AI技术的图片创作软件Imagen Editor&#xff0c;这款软件成熟以后&#xff0c;或将助力图片的创作。 近日有消息称&#xff0c;谷歌正在研发一款名为Imagen Editor生成式AI工具&…

设施管理系统

随着经济的快速发展&#xff0c;各种基础设施都在更新&#xff0c;在企事业单位中各种设施也都难以管理&#xff0c;以往传统的管理模式已经无法适应现代社会的需求&#xff0c;设备管理的滞后反而会影响设施设备的使用效果&#xff0c;因此设施设备管理系统必不可少。那么什么…

uniapp实现单选以及多选

<!-- 多选 --> <template><view class"choiceSupervisionLinkman"><view class"searchBox"><uni-search-bar :radius"8" placeholder"请输入成员名称" clearButton"none" confirm"search&q…