|
| 1 | +const log = (() => { |
| 2 | + return (...args) => { |
| 3 | + process.stdout.write(args.map(arg => |
| 4 | + typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg) |
| 5 | + ).join(' ') + '\n'); |
| 6 | + }; |
| 7 | +})(); |
| 8 | + |
| 9 | +const warn = (() => { |
| 10 | + return (...args) => { |
| 11 | + process.stderr.write(args.map(arg => |
| 12 | + typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg) |
| 13 | + ).join(' ') + '\n'); |
| 14 | + }; |
| 15 | +})(); |
| 16 | + |
1 | 17 | class Logger { |
2 | 18 | constructor(command = 'default') { |
3 | | - this.command = command |
4 | | - this.hasColors = this.checkColorSupport() |
| 19 | + this.command = command; |
| 20 | + this.colors = null; |
| 21 | + this.hasColors = false; |
| 22 | + this.initColors(); |
5 | 23 | } |
6 | 24 |
|
7 | | - checkColorSupport() { |
| 25 | + async initColors() { |
8 | 26 | try { |
9 | | - require('colors') |
10 | | - return true |
11 | | - } catch (e) { |
12 | | - console.warn('colors package not found, using basic logging') |
13 | | - return false |
| 27 | + const colorsModule = await import('colors'); |
| 28 | + this.colors = colorsModule.default || colorsModule; |
| 29 | + this.hasColors = true; |
| 30 | + } catch (err) { |
| 31 | + warn('colors package not found, using basic logging'); |
| 32 | + this.hasColors = false; |
14 | 33 | } |
15 | 34 | } |
16 | 35 |
|
17 | | - output(type, ...args) { // 支持多个参数 |
18 | | - const time = new Date().toLocaleTimeString() |
19 | | - const prefix = `[${this.command}] [${time}]` |
20 | | - |
21 | | - // 将所有参数合并为一个字符串 |
22 | | - const message = args.map(arg => { |
| 36 | + output(type, ...args) { |
| 37 | + const time = new Date().toLocaleTimeString(); |
| 38 | + const prefix = `[${this.command}] [${time}]`; |
| 39 | + const message = args.map((arg) => { |
23 | 40 | if (typeof arg === 'object') { |
24 | | - return JSON.stringify(arg, null, 2) |
| 41 | + return JSON.stringify(arg, null, 2); |
25 | 42 | } |
26 | | - return String(arg) |
27 | | - }).join(' ') |
| 43 | + return String(arg); |
| 44 | + }).join(' '); |
| 45 | + |
| 46 | + const outputFn = type === 'error' || type === 'warn' ? warn : log; |
28 | 47 |
|
29 | | - if (this.hasColors) { |
30 | | - const colors = require('colors') |
| 48 | + if (this.hasColors && this.colors) { |
31 | 49 | const colorMap = { |
32 | | - info: colors.cyan, |
33 | | - warn: colors.yellow, |
34 | | - error: colors.red, |
35 | | - success: colors.green |
36 | | - } |
| 50 | + info: this.colors.cyan, |
| 51 | + warn: this.colors.yellow, |
| 52 | + error: this.colors.red, |
| 53 | + success: this.colors.green, |
| 54 | + }; |
| 55 | + const coloredType = colorMap[type] |
| 56 | + ? colorMap[type](type.toUpperCase()) |
| 57 | + : type.toUpperCase(); |
37 | 58 |
|
38 | | - const coloredType = colorMap[type] ? colorMap[type](type.toUpperCase()) : type.toUpperCase() |
39 | | - console.log(`${prefix} ${coloredType} ${message}`) |
| 59 | + outputFn(`${prefix} ${coloredType} ${message}`); |
40 | 60 | } else { |
41 | 61 | const emojiMap = { |
42 | 62 | info: 'ℹ️', |
43 | 63 | warn: '⚠️', |
44 | 64 | error: '❌', |
45 | | - success: '✅' |
46 | | - } |
47 | | - console.log(`${prefix} ${emojiMap[type] || ''} ${message}`) |
| 65 | + success: '✅', |
| 66 | + }; |
| 67 | + outputFn(`${prefix} ${emojiMap[type] || ''} ${message}`); |
48 | 68 | } |
49 | 69 | } |
50 | 70 |
|
51 | 71 | success(...args) { |
52 | | - this.output('success', ...args) |
| 72 | + this.output('success', ...args); |
53 | 73 | } |
54 | 74 |
|
55 | 75 | info(...args) { |
56 | | - this.output('info', ...args) |
| 76 | + this.output('info', ...args); |
57 | 77 | } |
58 | 78 |
|
59 | 79 | warn(...args) { |
60 | | - this.output('warn', ...args) |
| 80 | + this.output('warn', ...args); |
61 | 81 | } |
62 | 82 |
|
63 | 83 | error(...args) { |
64 | | - this.output('error', ...args) |
| 84 | + this.output('error', ...args); |
65 | 85 | } |
66 | 86 | } |
67 | | -export default Logger |
| 87 | + |
| 88 | +export default Logger; |
0 commit comments