提交 | 用户 | 时间
|
820397
|
1 |
const isArray = function (obj: any): boolean { |
H |
2 |
return Object.prototype.toString.call(obj) === '[object Array]' |
|
3 |
} |
|
4 |
|
|
5 |
const Logger = () => {} |
|
6 |
|
|
7 |
Logger.typeColor = function (type: string) { |
|
8 |
let color = '' |
|
9 |
switch (type) { |
|
10 |
case 'primary': |
|
11 |
color = '#2d8cf0' |
|
12 |
break |
|
13 |
case 'success': |
|
14 |
color = '#19be6b' |
|
15 |
break |
|
16 |
case 'info': |
|
17 |
color = '#909399' |
|
18 |
break |
|
19 |
case 'warn': |
|
20 |
color = '#ff9900' |
|
21 |
break |
|
22 |
case 'error': |
|
23 |
color = '#f03f14' |
|
24 |
break |
|
25 |
default: |
|
26 |
color = '#35495E' |
|
27 |
break |
|
28 |
} |
|
29 |
return color |
|
30 |
} |
|
31 |
|
|
32 |
Logger.print = function (type = 'default', text: any, back = false) { |
|
33 |
if (typeof text === 'object') { |
|
34 |
// 如果是對象則調用打印對象方式 |
|
35 |
isArray(text) ? console.table(text) : console.dir(text) |
|
36 |
return |
|
37 |
} |
|
38 |
if (back) { |
|
39 |
// 如果是打印帶背景圖的 |
|
40 |
console.log( |
|
41 |
`%c ${text} `, |
|
42 |
`background:${Logger.typeColor(type)}; padding: 2px; border-radius: 4px; color: #fff;` |
|
43 |
) |
|
44 |
} else { |
|
45 |
console.log( |
|
46 |
`%c ${text} `, |
|
47 |
`border: 1px solid ${Logger.typeColor(type)}; |
|
48 |
padding: 2px; border-radius: 4px; |
|
49 |
color: ${Logger.typeColor(type)};` |
|
50 |
) |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
Logger.printBack = function (type = 'primary', text) { |
|
55 |
this.print(type, text, true) |
|
56 |
} |
|
57 |
|
|
58 |
Logger.pretty = function (type = 'primary', title, text) { |
|
59 |
if (typeof text === 'object') { |
|
60 |
console.group('Console Group', title) |
|
61 |
console.log( |
|
62 |
`%c ${title}`, |
|
63 |
`background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)}; |
|
64 |
padding: 1px; border-radius: 4px; color: #fff;` |
|
65 |
) |
|
66 |
isArray(text) ? console.table(text) : console.dir(text) |
|
67 |
console.groupEnd() |
|
68 |
return |
|
69 |
} |
|
70 |
console.log( |
|
71 |
`%c ${title} %c ${text} %c`, |
|
72 |
`background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)}; |
|
73 |
padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`, |
|
74 |
`border:1px solid ${Logger.typeColor(type)}; |
|
75 |
padding: 1px; border-radius: 0 4px 4px 0; color: ${Logger.typeColor(type)};`, |
|
76 |
'background:transparent' |
|
77 |
) |
|
78 |
} |
|
79 |
|
|
80 |
Logger.prettyPrimary = function (title, ...text) { |
|
81 |
text.forEach((t) => this.pretty('primary', title, t)) |
|
82 |
} |
|
83 |
|
|
84 |
Logger.prettySuccess = function (title, ...text) { |
|
85 |
text.forEach((t) => this.pretty('success', title, t)) |
|
86 |
} |
|
87 |
|
|
88 |
Logger.prettyWarn = function (title, ...text) { |
|
89 |
text.forEach((t) => this.pretty('warn', title, t)) |
|
90 |
} |
|
91 |
|
|
92 |
Logger.prettyError = function (title, ...text) { |
|
93 |
text.forEach((t) => this.pretty('error', title, t)) |
|
94 |
} |
|
95 |
|
|
96 |
Logger.prettyInfo = function (title, ...text) { |
|
97 |
text.forEach((t) => this.pretty('info', title, t)) |
|
98 |
} |
|
99 |
|
|
100 |
export default Logger |