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 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome
鱼樱前端5 小时前
深入JavaScript引擎与模块加载机制:从V8原理到模块化实战
前端·javascript