console.log封装

console.log封装

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

js 复制代码
/**
 * 检查给定的对象是否为数组
 */
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'
      break
    case 'success':
      color = '#19be6b'
      break
    case 'info':
      color = '#909399'
      break
    case 'warn':
      color = '#ff9900'
      break
    case 'error':
      color = '#f03f14'
      break
    default:
      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
相关推荐
仟濹4 小时前
【HTML】基础学习【数据分析全栈攻略:爬虫+处理+可视化+报告】
大数据·前端·爬虫·数据挖掘·数据分析·html
小小小小宇5 小时前
前端WebWorker笔记总结
前端
小小小小宇5 小时前
前端监控用户停留时长
前端
小小小小宇5 小时前
前端性能监控笔记
前端
烛阴6 小时前
Date-fns教程:现代JavaScript日期处理从入门到精通
前端·javascript
全栈小56 小时前
【前端】Vue3+elementui+ts,TypeScript Promise<string>转string错误解析,习惯性请出DeepSeek来解答
前端·elementui·typescript·vue3·同步异步
穗余6 小时前
NodeJS全栈开发面试题讲解——P6安全与鉴权
前端·sql·xss
小蜜蜂嗡嗡7 小时前
flutter项目迁移空安全
javascript·安全·flutter
穗余8 小时前
NodeJS全栈开发面试题讲解——P2Express / Nest 后端开发
前端·node.js
航Hang*8 小时前
WEBSTORM前端 —— 第3章:移动 Web —— 第4节:移动适配-VM
前端·笔记·edge·less·css3·html5·webstorm