当前位置: 首页 > news >正文

console.log封装

console.log封装

在控制台中打印带有颜色和格式的日志信息。

/*** 检查给定的对象是否为数组*/
const isArray = function (obj: any): boolean {return Object.prototype.toString.call(obj) === '[object Array]'
}/*** Logger 构造函数*/
Logger = () => {}/*** 根据日志类型返回对应的颜色*/
Logger.typeColor = function (type: string) {let color = ''switch (type) {case 'primary':color = '#2d8cf0'breakcase 'success':color = '#19be6b'breakcase 'info':color = '#909399'breakcase 'warn':color = '#ff9900'breakcase 'error':color = '#f03f14'breakdefault:color = '#35495E'break}return color
}/*** 打印日志信息,可选择是否带有背景色*/
Logger.print = function (type = 'default', text: any, back = false) {if (typeof text === 'object') {// 如果是對象則調用打印對象方式isArray(text) ? console.table(text) : console.dir(text)return}if (back) {// 如果是打印帶背景圖的console.log(`%c ${text} `,`background:${Logger.typeColor(type)}; padding: 2px; border-radius: 4px; color: #fff;`)} else {console.log(`%c ${text} `,`border: 1px solid ${Logger.typeColor(type)};padding: 2px; border-radius: 4px;color: ${Logger.typeColor(type)};`)}
}/*** 打印带有背景色的日志信息*/
Logger.printBack = function (type = 'primary', text) {this.print(type, text, true)
}/*** 打印格式化的日志信息,可选择是否带有背景色*/
Logger.pretty = function (type = 'primary', title, text) {if (typeof text === 'object') {console.group('Console Group', title)console.log(`%c ${title}`,`background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`)isArray(text) ? console.table(text) : console.dir(text)console.groupEnd()return}console.log(`%c ${title} %c ${text} %c`,`background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`,`border:1px solid ${Logger.typeColor(type)};padding: 1px; border-radius: 0 4px 4px 0; color: ${Logger.typeColor(type)};`,'background:transparent')
}/*** 使用 primary 类型打印格式化的日志信息*/
Logger.prettyPrimary = function (title, ...text) {text.forEach((t) => this.pretty('primary', title, t))
}/*** 使用 success 类型打印格式化的日志信息*/
Logger.prettySuccess = function (title, ...text) {text.forEach((t) => this.pretty('success', title, t))
}/*** 使用 warn 类型打印格式化的日志信息*/
Logger.prettyWarn = function (title, ...text) {text.forEach((t) => this.pretty('warn', title, t))
}/*** 使用 error 类型打印格式化的日志信息*/
Logger.prettyError = function (title, ...text) {text.forEach((t) => this.pretty('error', title, t))
}/*** 使用 info 类型打印格式化的日志信息*/
Logger.prettyInfo = function (title, ...text) {text.forEach((t) => this.pretty('info', title, t))
}export default Logger
http://www.lryc.cn/news/516577.html

相关文章:

  • 戴尔/Dell 电脑按什么快捷键可以进入 Bios 设置界面?
  • GitLab创建用户,设置访问SSH Key
  • Mysql--基础篇--SQL(DDL,DML,窗口函数,CET,视图,存储过程,触发器等)
  • 比较 FreeSWITCH 的 asr 事件和回调函数
  • 基于ffmpeg和sdl2的简单视频播放器制作
  • 卫星导航信号的形成及解算
  • 硬件-射频-PCB-常见天线分类-ESP32实例
  • salesforce 验证规则判断一个picklist是否为none
  • 解决 IntelliJ IDEA 中 Tomcat 日志乱码问题的详细指南
  • 如何分析 Nginx 日志
  • Kubernetes Gateway API-5-后端协议和网关基础设置标签
  • 大数据架构演变
  • Bash语言的软件工程
  • OpenGL —— 流媒体播放器 - ffmpeg解码rtsp流,opengl渲染yuv视频(附源码,glfw+glad)
  • CE中注册的符号地址如何通过编程获取
  • Math Reference Notes: 积分因子
  • 解决7-Zip图标更换问题
  • Java 性能监控工具详解:JConsole、VisualVM 和 Java Mission Control
  • 浏览器报错:您的连接不是私密连接,Kubernetes Dashboard无法打开
  • 用Python进行大数据处理:如何使用pandas和dask处理海量数据
  • 机器人手眼标定
  • 基于Springboot + vue实现的校园失物招领系统
  • 关于C语言初步的一些基础知识整理(2)
  • Linux驱动开发:深入理解I2C时序(二)
  • 逆向安卓抓包
  • Spring源码分析之事件机制——观察者模式(一)
  • QT实现 端口扫描暂停和继续功能 3
  • SHViT模型详解
  • QGIS Server安装部署教程
  • 基于 Apache Commons Pool 实现的 gRPC 连接池管理类 GrpcChannelPool 性能分析与优化