log.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const prettyLog = () => {
  2. const isProduction = import.meta.env.MODE === 'production';
  3. const isEmpty = (value: any) => {
  4. return value == null || value === undefined || value === '';
  5. };
  6. const prettyPrint = (title: string, text: string, color: string) => {
  7. if (isProduction) return;
  8. console.log(
  9. `%c ${title} %c ${text} %c`,
  10. `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
  11. `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,
  12. 'background:transparent'
  13. );
  14. };
  15. const info = (textOrTitle: string, content = '') => {
  16. const title = isEmpty(content) ? 'Info' : textOrTitle;
  17. const text = isEmpty(content) ? textOrTitle : content;
  18. prettyPrint(title, text, '#909399');
  19. };
  20. const error = (textOrTitle: string, content = '') => {
  21. const title = isEmpty(content) ? 'Error' : textOrTitle;
  22. const text = isEmpty(content) ? textOrTitle : content;
  23. prettyPrint(title, text, '#F56C6C');
  24. };
  25. const warning = (textOrTitle: string, content = '') => {
  26. const title = isEmpty(content) ? 'Warning' : textOrTitle;
  27. const text = isEmpty(content) ? textOrTitle : content;
  28. prettyPrint(title, text, '#E6A23C');
  29. };
  30. const success = (textOrTitle: string, content = '') => {
  31. const title = isEmpty(content) ? 'Success ' : textOrTitle;
  32. const text = isEmpty(content) ? textOrTitle : content;
  33. prettyPrint(title, text, '#67C23A');
  34. };
  35. const table = () => {
  36. const data = [
  37. { id: 1, name: 'Alice', age: 25 },
  38. { id: 2, name: 'Bob', age: 30 },
  39. { id: 3, name: 'Charlie', age: 35 }
  40. ];
  41. console.log(
  42. '%c id%c name%c age',
  43. 'color: white; background-color: black; padding: 2px 10px;',
  44. 'color: white; background-color: black; padding: 2px 10px;',
  45. 'color: white; background-color: black; padding: 2px 10px;'
  46. );
  47. data.forEach((row: any) => {
  48. console.log(
  49. `%c ${row.id} %c ${row.name} %c ${row.age} `,
  50. 'color: black; background-color: lightgray; padding: 2px 10px;',
  51. 'color: black; background-color: lightgray; padding: 2px 10px;',
  52. 'color: black; background-color: lightgray; padding: 2px 10px;'
  53. );
  54. });
  55. };
  56. const picture = (url: string, scale = 1) => {
  57. if (isProduction) return;
  58. const img = new Image();
  59. img.crossOrigin = 'anonymous';
  60. img.onload = () => {
  61. const c = document.createElement('canvas');
  62. const ctx = c.getContext('2d');
  63. if (ctx) {
  64. c.width = img.width;
  65. c.height = img.height;
  66. ctx.fillStyle = 'red';
  67. ctx.fillRect(0, 0, c.width, c.height);
  68. ctx.drawImage(img, 0, 0);
  69. const dataUri = c.toDataURL('image/png');
  70. console.log(
  71. `%c sup?`,
  72. `font-size: 1px;
  73. padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
  74. background-image: url(${dataUri});
  75. background-repeat: no-repeat;
  76. background-size: ${img.width * scale}px ${img.height * scale}px;
  77. color: transparent;
  78. `
  79. );
  80. }
  81. };
  82. img.src = url;
  83. };
  84. // retu;
  85. return {
  86. info,
  87. error,
  88. warning,
  89. success,
  90. picture,
  91. table
  92. };
  93. };
  94. export const log = prettyLog();
  95. log.info('这是基础信息!');
  96. log.info('注意看', '这是个男人叫小帅!');
  97. log.error('奥德彪', '出来的时候穷 生活总是让我穷 所以现在还是穷。');
  98. log.error('前方的路看似很危险,实际一点也不安全。');
  99. log.warning('奥德彪', '我并非无路可走 我还有死路一条! ');
  100. log.success('奥德彪', '钱没了可以再赚,良心没了便可以赚的更多。 ');
  101. 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')