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
相关推荐
喝拿铁写前端2 小时前
前端与 AI 结合的 10 个可能路径图谱
前端·人工智能
codingandsleeping2 小时前
浏览器的缓存机制
前端·后端
-代号95273 小时前
【JavaScript】十二、定时器
开发语言·javascript·ecmascript
灵感__idea4 小时前
JavaScript高级程序设计(第5版):扎实的基本功是唯一捷径
前端·javascript·程序员
摇滚侠4 小时前
Vue3 其它API toRow和markRow
前端·javascript
難釋懷4 小时前
JavaScript基础-history 对象
开发语言·前端·javascript
beibeibeiooo4 小时前
【CSS3】04-标准流 + 浮动 + flex布局
前端·html·css3
拉不动的猪4 小时前
刷刷题47(react常规面试题2)
前端·javascript·面试
浪遏4 小时前
场景题:大文件上传 ?| 过总字节一面😱
前端·javascript·面试
计算机毕设定制辅导-无忧学长4 小时前
HTML 与 JavaScript 交互:学习进程中的新跨越(一)
javascript·html·交互