@aiot-toolkit/shared-utils 2.0.3-beta.1 → 2.0.3-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ColorConsole.d.ts +22 -0
- package/lib/ColorConsole.js +58 -6
- package/package.json +2 -2
package/lib/ColorConsole.d.ts
CHANGED
|
@@ -44,5 +44,27 @@ declare class ColorConsole {
|
|
|
44
44
|
* @returns 返回打印的日志信息
|
|
45
45
|
*/
|
|
46
46
|
static logger(log: ILog): void;
|
|
47
|
+
/**
|
|
48
|
+
* 日志转换为带颜色的字符串
|
|
49
|
+
*
|
|
50
|
+
* @param log 日志对象
|
|
51
|
+
* @param prefix 前缀列表
|
|
52
|
+
* false-不使用前缀
|
|
53
|
+
* string[]-自定义前缀
|
|
54
|
+
* 默认-[图标、时间、toolkit字样]
|
|
55
|
+
*
|
|
56
|
+
* @example logToString(log, ['myPrefix'])
|
|
57
|
+
*/
|
|
58
|
+
static logToString(log: ILog, prefix?: string[] | false): string;
|
|
59
|
+
/**
|
|
60
|
+
* 创建默认消息前缀列表
|
|
61
|
+
*
|
|
62
|
+
* 1. 图标
|
|
63
|
+
* 2. 时间
|
|
64
|
+
* 3. toolkit 字样
|
|
65
|
+
*
|
|
66
|
+
* @param log
|
|
67
|
+
*/
|
|
68
|
+
private static createDefaultPrefix;
|
|
47
69
|
}
|
|
48
70
|
export default ColorConsole;
|
package/lib/ColorConsole.js
CHANGED
|
@@ -55,9 +55,7 @@ class ColorConsole {
|
|
|
55
55
|
}
|
|
56
56
|
static _log(logLevel, ...message) {
|
|
57
57
|
const style = this.styleDic[logLevel] || this.defaultStyle;
|
|
58
|
-
|
|
59
|
-
const icon = style.icon ? style.icon : '';
|
|
60
|
-
console.log(icon, time, this.createMessage(message.map((item) => {
|
|
58
|
+
console.log(...this.createDefaultPrefix({ level: logLevel, message }), this.createMessage(message.map((item) => {
|
|
61
59
|
if (typeof item === 'object') {
|
|
62
60
|
return Object.assign(Object.assign({}, item), { style: item.style || style });
|
|
63
61
|
}
|
|
@@ -90,10 +88,26 @@ class ColorConsole {
|
|
|
90
88
|
* @returns 返回打印的日志信息
|
|
91
89
|
*/
|
|
92
90
|
static logger(log) {
|
|
91
|
+
const str = this.logToString(log);
|
|
92
|
+
console.log(str);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 日志转换为带颜色的字符串
|
|
96
|
+
*
|
|
97
|
+
* @param log 日志对象
|
|
98
|
+
* @param prefix 前缀列表
|
|
99
|
+
* false-不使用前缀
|
|
100
|
+
* string[]-自定义前缀
|
|
101
|
+
* 默认-[图标、时间、toolkit字样]
|
|
102
|
+
*
|
|
103
|
+
* @example logToString(log, ['myPrefix'])
|
|
104
|
+
*/
|
|
105
|
+
static logToString(log, prefix) {
|
|
93
106
|
const { message, position, filePath, level = ILog_1.Loglevel.INFO } = log;
|
|
107
|
+
// 位置消息
|
|
94
108
|
let positionMessage;
|
|
95
109
|
positionMessage = {
|
|
96
|
-
style: this.getStyle(
|
|
110
|
+
style: this.getStyle(level),
|
|
97
111
|
word: [filePath, position === null || position === void 0 ? void 0 : position.startLine, position === null || position === void 0 ? void 0 : position.startColumn]
|
|
98
112
|
.filter((item) => Boolean(item))
|
|
99
113
|
.join(':')
|
|
@@ -102,7 +116,45 @@ class ColorConsole {
|
|
|
102
116
|
if (positionMessage) {
|
|
103
117
|
logs.unshift(positionMessage);
|
|
104
118
|
}
|
|
105
|
-
|
|
119
|
+
// 前缀
|
|
120
|
+
let effectPrefix;
|
|
121
|
+
if (prefix === false) {
|
|
122
|
+
prefix = [];
|
|
123
|
+
}
|
|
124
|
+
if (prefix === undefined) {
|
|
125
|
+
effectPrefix = _a.createDefaultPrefix(log);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
effectPrefix = prefix;
|
|
129
|
+
}
|
|
130
|
+
const style = this.styleDic[level] || this.defaultStyle;
|
|
131
|
+
return [
|
|
132
|
+
...effectPrefix,
|
|
133
|
+
this.createMessage(logs.map((item) => {
|
|
134
|
+
if (typeof item === 'object') {
|
|
135
|
+
return Object.assign(Object.assign({}, item), {
|
|
136
|
+
// 对象格式消息的样式依次使用:[消息自身样式, log样式, 默认样式]
|
|
137
|
+
style: item.style || style });
|
|
138
|
+
}
|
|
139
|
+
return item;
|
|
140
|
+
}))
|
|
141
|
+
].join(' ');
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 创建默认消息前缀列表
|
|
145
|
+
*
|
|
146
|
+
* 1. 图标
|
|
147
|
+
* 2. 时间
|
|
148
|
+
* 3. toolkit 字样
|
|
149
|
+
*
|
|
150
|
+
* @param log
|
|
151
|
+
*/
|
|
152
|
+
static createDefaultPrefix(log) {
|
|
153
|
+
const { level = ILog_1.Loglevel.INFO } = log;
|
|
154
|
+
const style = this.styleDic[level] || this.defaultStyle;
|
|
155
|
+
const time = this.formatWord(`[${(0, dayjs_1.default)().format('MM-DD HH:mm:ss')}] [toolkit]:`, style);
|
|
156
|
+
const icon = style.icon ? style.icon : '';
|
|
157
|
+
return [icon, time];
|
|
106
158
|
}
|
|
107
159
|
}
|
|
108
160
|
_a = ColorConsole;
|
|
@@ -116,7 +168,7 @@ ColorConsole.styleDic = {
|
|
|
116
168
|
},
|
|
117
169
|
[ILog_1.Loglevel.INFO]: {
|
|
118
170
|
color: '#03A9F4',
|
|
119
|
-
icon: '
|
|
171
|
+
icon: '🔷'
|
|
120
172
|
},
|
|
121
173
|
[ILog_1.Loglevel.WARN]: {
|
|
122
174
|
color: '#FF9800',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/shared-utils",
|
|
3
|
-
"version": "2.0.3-beta.
|
|
3
|
+
"version": "2.0.3-beta.3",
|
|
4
4
|
"description": "The common method functions of the aiot-toolkit.",
|
|
5
5
|
"homepage": "",
|
|
6
6
|
"license": "ISC",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"@types/minimatch": "^5.1.2",
|
|
42
42
|
"@types/qrcode": "^1.5.5"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "a4cad870b23d9e4142d22e59aa5618d7f123e1ab"
|
|
45
45
|
}
|