浏览器花式打印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>
相关推荐
qiuhaipeng126 分钟前
Claude code 升级后上下文长度变短问题
java·前端·数据库
zzz_236831 分钟前
个人 AI 记忆如何跨工具复用:用 Markdown、索引和 Skill 搭一个可治理的记忆库
前端·人工智能·react.js·前端框架·agent·agent测评
剑之所向2 小时前
.NET 官方`System.Threading.Channels`
前端·javascript·数据库
计算机魔术师3 小时前
GPT-6 Astra幻觉砍到2%,却被一种老招数轻松绕过
前端
cjy0001113 小时前
2026AI 产品如何从一次性 Demo 变成可重复使用的业务工具?
前端·人工智能·fde
IT_陈寒3 小时前
React的状态更新坑得我差点加班到天亮
前端·人工智能·后端
风骏时光牛马3 小时前
深挖底层逻辑:XX源码深度拆解分析
前端
渡我白衣4 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
电商API_180079052474 小时前
电商商品价格监控工具淘宝京东商品价格抓取API项目实操分享
java·大数据·开发语言·前端·数据挖掘
sxmas4 小时前
用状态机与DAG建模四阶段业务流程:以MIND模型为例
前端