Skip to content

Commit 7254412

Browse files
committed
fix: modify log output
1 parent f6861e8 commit 7254412

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

designer-demo/scripts/logger.mjs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,67 @@
1-
import colors from 'picocolors'
2-
31
class Logger {
4-
constructor(command) {
2+
constructor(command = 'default') {
53
this.command = command
4+
this.hasColors = this.checkColorSupport()
65
}
76

8-
output(type, msg) {
9-
const format = () => {
7+
checkColorSupport() {
8+
try {
9+
require('colors')
10+
return true
11+
} catch (e) {
12+
console.warn('colors package not found, using basic logging')
13+
return false
14+
}
15+
}
16+
17+
output(type, ...args) { // 支持多个参数
18+
const time = new Date().toLocaleTimeString()
19+
const prefix = `[${this.command}] [${time}]`
20+
21+
// 将所有参数合并为一个字符串
22+
const message = args.map(arg => {
23+
if (typeof arg === 'object') {
24+
return JSON.stringify(arg, null, 2)
25+
}
26+
return String(arg)
27+
}).join(' ')
28+
29+
if (this.hasColors) {
30+
const colors = require('colors')
1031
const colorMap = {
11-
info: 'cyan',
12-
warn: 'yellow',
13-
error: 'red',
14-
success: 'green'
32+
info: colors.cyan,
33+
warn: colors.yellow,
34+
error: colors.red,
35+
success: colors.green
1536
}
16-
const time = new Date().toLocaleTimeString()
17-
const colorMsg = colors[colorMap[type]](type)
1837

19-
return `[${this.command}] [${colors.dim(time)}] ${colorMsg} ${msg}`
38+
const coloredType = colorMap[type] ? colorMap[type](type.toUpperCase()) : type.toUpperCase()
39+
console.log(`${prefix} ${coloredType} ${message}`)
40+
} else {
41+
const emojiMap = {
42+
info: 'ℹ️',
43+
warn: '⚠️',
44+
error: '❌',
45+
success: '✅'
46+
}
47+
console.log(`${prefix} ${emojiMap[type] || ''} ${message}`)
2048
}
21-
const _logger = console
22-
23-
return _logger.log(format())
2449
}
2550

26-
info(msg) {
27-
this.output('info', msg)
51+
success(...args) {
52+
this.output('success', ...args)
2853
}
2954

30-
warn(msg) {
31-
this.output('warn', msg)
55+
info(...args) {
56+
this.output('info', ...args)
3257
}
3358

34-
error(msg) {
35-
this.output('error', msg)
59+
warn(...args) {
60+
this.output('warn', ...args)
3661
}
3762

38-
success(msg) {
39-
this.output('success', msg)
63+
error(...args) {
64+
this.output('error', ...args)
4065
}
4166
}
42-
4367
export default Logger

designer-demo/scripts/uploadMaterials.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ async function main() {
5353
throw new Error(`Upload failed with status ${response.status}: ${errorText}`)
5454
}
5555
const data = await response.json()
56-
logger.success('File uploaded successfully:', data)
56+
if (data && data.success) {
57+
logger.success('File uploaded successfully!')
58+
logger.success('Inserted records:', data.data?.insertNum || 0)
59+
logger.success('Updated records:', data.data?.updateNum || 0)
60+
logger.success('Message:', data.message)
61+
} else {
62+
logger.warn('Upload completed but success flag is false:', data)
63+
logger.warn('Upload completed with warnings:', data.message)
64+
}
5765
} catch (error) {
5866
logger.error('Error uploading file:', error instanceof Error ? error.message : String(error))
5967
}

0 commit comments

Comments
 (0)