| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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
- };
- };
- export const log = prettyLog();
- log.info('这是基础信息!');
- log.info('注意看', '这是个男人叫小帅!');
- log.error('奥德彪', '出来的时候穷 生活总是让我穷 所以现在还是穷。');
- log.error('前方的路看似很危险,实际一点也不安全。');
- log.warning('奥德彪', '我并非无路可走 我还有死路一条! ');
- log.success('奥德彪', '钱没了可以再赚,良心没了便可以赚的更多。 ');
- log.picture('https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F0514%2Fd0ea93ebj00sdgx56001xd200u000gtg00hz00a2.jpg&thumbnail=660x2147483647&quality=80&type=jpg')
|