浏览器花式打印console.log

console是一个用于调试和记录信息的内置对象, 提供了多种方法,可以帮助开发者输出各种信息,进行调试和分析。

美化后的效果:

美化日志工具类

javascript 复制代码
const isArray = function (obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}

const Logger = function () {
};

Logger.typeColor = function (type) {
  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) {
  if (typeof text === 'object') {
    // 如果是对象则调用打印对象方式
    isArray(text) ? console.table(text) : console.dir(text);
    return;
  }
  console.log(
    `%c ${text} `,
    `border: 1px solid ${Logger.typeColor(type)};
        padding: 2px; border-radius: 4px;
        color: ${Logger.typeColor(type)};`
  );
}

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; 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'
  );
}

Logger.prettyPrimary = function (title, ...text) {
  text.forEach((t) => this.pretty('primary', title, t));
}

Logger.prettySuccess = function (title, ...text) {
  text.forEach((t) => this.pretty('success', title, t));
}

Logger.prettyWarn = function (title, ...text) {
  text.forEach((t) => this.pretty('warn', title, t));
}

Logger.prettyError = function (title, ...text) {
  text.forEach((t) => this.pretty('error', title, t));
}

Logger.prettyInfo = function (title, ...text) {
  text.forEach((t) => this.pretty('info', title, t));
}

/**
 * 打印图片
 * @param url 图片地址
 * @param scale 图片缩放比例
 */
Logger.printPic = function (url, scale = 1) {
  const img = new Image();
  img.crossOrigin = 'anonymous';
  img.onload = () => {
    const c = document.createElement('canvas');
    const ctx = c.getContext('2d');
    if (ctx) {
      c.width = img.width;
      c.height = img.height;
      ctx.fillStyle = 'red';
      ctx.fillRect(0, 0, c.width, c.height);
      ctx.drawImage(img, 0, 0);
      const dataUri = c.toDataURL('image/png');
      console.log(
        `%c sup?`,
        `font-size: 1px;
                padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
                background-image: url(${dataUri});
                background-repeat: no-repeat;
                background-size: ${img.width * scale}px ${img.height * scale}px;
                color: transparent;
                `
      );
    }
  };
  img.src = url;
}

export default Logger;

Vue2中的使用

main.js

javascript 复制代码
// 引入prettyLog.js
import Logger from "./utils/prettyLog";
// 将 Logger 挂载到 Vue 原型上
Vue.prototype.$logger = Logger;

index.vue

html 复制代码
<template>
  <div>
    <div align="center">
      <div class="typing">
        欢迎使用木芒果有限公司后台管理系统
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "Index",
  data() {
    return {
      
    }
  },
  created() {
    //打印标准日志
    this.$logger.prettyPrimary("欢迎使用!", "木芒果有限公司后台管理系统")
    //打印信息日志
    this.$logger.prettyInfo("欢迎使用!", "木芒果有限公司后台管理系统")
    //打印成功日志
    this.$logger.prettySuccess("欢迎使用!", "木芒果有限公司后台管理系统")
    //打印错误日志
    this.$logger.prettyError("欢迎使用!", "木芒果有限公司后台管理系统")
    //打印警告日志
    this.$logger.prettyWarn("欢迎使用!", "木芒果有限公司后台管理系统")
    //打印带图片的日志
    this.$logger.printPic("https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F0514%2Fd0ea93ebj00sdgx56001xd200u000gtg00hz00a2.jpg&thumbnail=660x2147483647&quality=80&type=jpg")
  }
}
</script>

<style scoped>
</style>

Vue3中的使用

main.js

javascript 复制代码
import Logger from "@/utils/prettyLog";
// 将 Logger 绑定到 window 对象上
window.logger = Logger;

index.vue

html 复制代码
<template>
  <div>
    <div align="center">
      <div class="typing">
        欢迎使用木芒果有限公司后台管理系统
      </div>
    </div>
  </div>
</template>

<script setup>
onMounted(() => {
  //打印标准日志
  window.logger.prettyPrimary("欢迎使用!", "木芒果有限公司后台管理系统")
  //打印信息日志
  window.logger.prettyInfo("欢迎使用!", "木芒果有限公司后台管理系统")
  //打印成功日志
  window.logger.prettySuccess("欢迎使用!", "木芒果有限公司后台管理系统")
  //打印错误日志
  window.logger.prettyError("欢迎使用!", "木芒果有限公司后台管理系统")
  //打印警告日志
  window.logger.prettyWarn("欢迎使用!", "木芒果有限公司后台管理系统")
  //打印带图片的日志
  window.logger.printPic("https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F0514%2Fd0ea93ebj00sdgx56001xd200u000gtg00hz00a2.jpg&thumbnail=660x2147483647&quality=80&type=jpg")
});
</script>
相关推荐
嫣嫣细语9 分钟前
css实现鼠标禁用(鼠标滑过显示红色禁止符号)
前端·css
Days205013 分钟前
web前端主要包括哪些技术
前端
NaRciCiSSuS31 分钟前
第一章-JavaScript简介
开发语言·javascript·ecmascript
XF鸭37 分钟前
HTML-CSS 入门介绍
服务器·前端·javascript
forwardMyLife1 小时前
element-plus 的form表单组件之el-radio(单选按钮组件)
前端·javascript·vue.js
fs哆哆2 小时前
ExcelVBA运用Excel的【条件格式】(二)
linux·运维·服务器·前端·excel
安冬的码畜日常2 小时前
【CSS in Depth 2精译】2.5 无单位的数值与行高
前端·css
ilisi_2 小时前
导航栏样式,盒子模型
前端·javascript·css
吉吉安2 小时前
grid布局下的展开/收缩过渡效果【vue/已验证可正常运行】
前端·javascript·vue.js
梦凡尘2 小时前
Vue3 对跳转 同一路由传入不同参数的页面分别进行缓存
前端·javascript·vue.js