Console.log 美化

news/2024/10/2 16:17:10

美化代码

// 美化打印实现方法
const prettyLog = () => {const isProduction = import.meta.env.MODE === "production";const isEmpty = (value: any) => {return value == null || value === undefined || value === "";};const prettyPrint = (title: string, text: string, color: string) => {if (isProduction) return;console.log(`%c ${title} %c ${text} %c`,`background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,`border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,"background:transparent");};const info = (textOrTitle: string, content = "") => {const title = isEmpty(content) ? "Info" : textOrTitle;const text = isEmpty(content) ? textOrTitle : content;prettyPrint(title, text, "#909399");};const error = (textOrTitle: string, content = "") => {const title = isEmpty(content) ? "Error" : textOrTitle;const text = isEmpty(content) ? textOrTitle : content;prettyPrint(title, text, "#F56C6C");};const warning = (textOrTitle: string, content = "") => {const title = isEmpty(content) ? "Warning" : textOrTitle;const text = isEmpty(content) ? textOrTitle : content;prettyPrint(title, text, "#E6A23C");};const success = (textOrTitle: string, content = "") => {const title = isEmpty(content) ? "Success" : textOrTitle;const text = isEmpty(content) ? textOrTitle : content;prettyPrint(title, text, "#67C23A");};const table = () => {const data = [{ id: 1, name: "Alice", age: 25 },{ id: 2, name: "Bob", age: 30 },{ id: 3, name: "Charlie", age: 35 }];console.log("%c id%c name%c age","color: white; background-color: black; padding: 2px 10px;","color: white; background-color: black; padding: 2px 10px;","color: white; background-color: black; padding: 2px 10px;");data.forEach((row: any) => {console.log(`%c ${row.id} %c ${row.name} %c ${row.age} `,"color: black; background-color: lightgray; padding: 2px 10px;","color: black; background-color: lightgray; padding: 2px 10px;","color: black; background-color: lightgray; padding: 2px 10px;");});};const picture = (url: string, scale = 1) => {if (isProduction) return;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;};// retu;return {info,error,warning,success,picture,table};
};
// 创建打印对象
const log = prettyLog();
// 不带标题
log.info("这是基础信息");
log.info("看这里", "这是基础信息");
log.error("这是错误信息");
log.error("出错啦", "这是错误信息");
log.success("这是成功信息");
log.success("成功啦", "这是成功信息");
log.warning("这是警告信息");
log.warning("注意了", "这是警告信息");
log.table();
log.picture("http://127.0.0.1:8520//media/generate/202404197020/ec0b5131-d0c5-4201-b1d3-94edcea2ba12.png",0.2
);

效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/44778.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

性能分析: 快速定位SQL问题

在数据库性能调优的实践中,SQL性能分析是至关重要的一环。一个执行效率低下的SQL语句可能会导致整个系统的性能瓶颈。 为了快速定位并解决这些问题,我们需要对SQL进行性能分析。本文将介绍一些常用的方法和技术,帮助大家快速定位SQL问题。 1、找出执行时间最长的SQL 首先,我…

IP地址按照数值大小进行排序

sort -n -t . -k1,1 -k2,2 -k3,3 -k4,4 ip.txt -n: 指示 sort 进行数值排序,而不是默认的字典序排序。 -t .: 设置字段的分隔符为点(.)。这通常用于处理IP地址或版本号等以点分隔的数值。 -k1,1 -k2,2 -k3,3 -k4,4: 这些是排序的关键部分,指定了排序的键。

坚持与确定性:毒药还是良药?

趁现在就改变,千万别让确定性这副慢性毒药继续侵蚀我们的思想。前段时间跟几个大龄程序员一起吃饭,聊了大家的现状,后来写了篇博客总结了一下《从大龄程序员现状聊聊出路》,本想着给朋友们提供些观点和思路,结果被有些网友批评了。 1. 我的认知达不到赚快钱 有的网友认为我…

UVM中打印信息的控制+内存分配算法+C语言fork()函数+使用uvm自带的reg做默认值检查+uvm_resource_db+ral手动更新+vim操作

UVM中打印信息的控制 非uvm平台控制的module中的uvm info使用*号通配,而找不到对应的uvm_top,起始点不是harness/top。使用ID可以匹配到对应的信息。 https://blog.csdn.net/Bonnie_89/article/details/128897812 使用uvm_set_action,将某ID和某严重性的行为重载// +uvm_set…

闲话 24.6.13

sif闲话 感觉写的有点长了,当半个社论发了吧( 没啥想写的闲话啊! 今日推歌:所以我放走了那天的流星 by 不鱼_P feat. 星尘 Minus Stabilized Interval Free 对两个正整数 \(l,r\),本文使用 \([l, r]\) 表示 \(\{x\in \mathbb N \mid l\le x \le r\}\)。 对排列 \(\pi = (\…

千万级流量冲击下,如何保证极致性能

1 简要介绍 随着互联网的快速发展,网络应用的流量规模不断攀升,特别是在电商大促、明星直播、重大赛事、头条热搜等热点事件中,秒级100w请求成为了常态。在这样的流量冲击下,如何确保系统稳定、高效地处理每一个请求,为用户提供极致的体验,成为了技术团队面临的重要挑战。…

SAP:SALV刷新按钮事件

SAP ECC6 SALV刷新按钮事件的实现SAP SALV刷新事件 1、主程序*&---------------------------------------------------------------------* *& Report Z17_04 *& *&---------------------------------------------------------------------* *& 全屏Clas…

解决github中一个新手著名问题

问题的引入 首先新手在初学git的时候,会先git init,然后config一些配置,之后链接远程仓库的时候,十分有可能碰到一个报错:ERROR: Permission to yixianshen-reserved/learning-repo.git denied to yiquanfeng. fatal: Could not read from remote repository. Please make…