@wiajs/log 4.3.15 → 4.3.16
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/dist/log.bs.cjs +155 -161
- package/dist/log.js +183 -181
- package/dist/log.mjs +155 -161
- package/lib/browser.js +57 -53
- package/lib/node.js +24 -18
- package/package.json +1 -1
package/dist/log.bs.cjs
CHANGED
|
@@ -13,135 +13,132 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
13
13
|
* m 为模块,fn 为函数名称
|
|
14
14
|
*/
|
|
15
15
|
class Log {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
args[0] = { exp: args[0].message };
|
|
127
|
-
_.error(...args);
|
|
128
|
-
}
|
|
16
|
+
/** @type {string} 模块 */
|
|
17
|
+
m = ''
|
|
18
|
+
|
|
19
|
+
/** @type {string} 函数 */
|
|
20
|
+
fn = ''
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} m 模块
|
|
24
|
+
*/
|
|
25
|
+
constructor(m) {
|
|
26
|
+
this.m = m;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* get log desc
|
|
31
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
|
32
|
+
* @param {*[]} args
|
|
33
|
+
* @returns {{desc: string, arg: *[]}}
|
|
34
|
+
*/
|
|
35
|
+
getDesc(args) {
|
|
36
|
+
let R = {desc: '', arg: args};
|
|
37
|
+
try {
|
|
38
|
+
const _ = this;
|
|
39
|
+
const {m} = _;
|
|
40
|
+
let fn = '';
|
|
41
|
+
let desc = '';
|
|
42
|
+
|
|
43
|
+
if (args.length > 1) {
|
|
44
|
+
const last = args.at(-1);
|
|
45
|
+
if (typeof last === 'object') {
|
|
46
|
+
;({desc, fn} = last);
|
|
47
|
+
} else if (typeof last === 'string') desc = last;
|
|
48
|
+
if (desc || fn) {
|
|
49
|
+
fn = fn || _.fn;
|
|
50
|
+
_.fn = fn;
|
|
51
|
+
args.pop();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
fn = fn || _.fn;
|
|
55
|
+
if (m) desc = `${desc}[${m}${fn ? `:${fn}` : ''}]`; // eslint-disable-line
|
|
56
|
+
R = {desc, arg: args};
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.error(`getDesc exp:${e.message}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return R
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @param {...any} args - params */
|
|
65
|
+
log(...args) {
|
|
66
|
+
const _ = this;
|
|
67
|
+
const last = args.at(-1);
|
|
68
|
+
// clear fn
|
|
69
|
+
if (args.length === 1 && typeof last === 'object' && last.fn) _.fn = '';
|
|
70
|
+
else {
|
|
71
|
+
const desc = _.getDesc(args);
|
|
72
|
+
console.log(desc, ...args);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {...any} args - params */
|
|
77
|
+
debug(...args) {
|
|
78
|
+
const _ = this;
|
|
79
|
+
const desc = _.getDesc(args);
|
|
80
|
+
if (desc) console.log(desc, ...args);
|
|
81
|
+
else console.log(...args);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** @param {...any} args - params */
|
|
85
|
+
info(...args) {
|
|
86
|
+
const _ = this;
|
|
87
|
+
const desc = _.getDesc(args);
|
|
88
|
+
if (desc) console.info(desc, ...args);
|
|
89
|
+
else console.log(...args);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @param {...any} args - params */
|
|
93
|
+
warn(...args) {
|
|
94
|
+
const _ = this;
|
|
95
|
+
const {desc, arg} = _.getDesc(args);
|
|
96
|
+
if (desc) console.warn(desc, ...arg);
|
|
97
|
+
else console.log(...args);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @param {...any} args - params */
|
|
101
|
+
trace(...args) {
|
|
102
|
+
const _ = this;
|
|
103
|
+
const {desc, arg} = _.getDesc(args);
|
|
104
|
+
if (desc) console.trace(desc, ...arg);
|
|
105
|
+
else console.trace(...args);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @param {...any} args - params */
|
|
109
|
+
error(...args) {
|
|
110
|
+
const _ = this;
|
|
111
|
+
const {desc, arg} = _.getDesc(args);
|
|
112
|
+
if (desc) console.error(desc, ...arg);
|
|
113
|
+
else console.log(...args);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 用于 catch(e) log.err(e)
|
|
118
|
+
* @param {...any} args - params */
|
|
119
|
+
err(...args) {
|
|
120
|
+
const _ = this;
|
|
121
|
+
const first = args?.[0];
|
|
122
|
+
if (first instanceof Error || (first?.message && first?.cause && first?.stack))
|
|
123
|
+
args[0] = {exp: args[0].message};
|
|
124
|
+
_.error(...args);
|
|
125
|
+
}
|
|
129
126
|
}
|
|
130
127
|
|
|
131
128
|
/**
|
|
132
129
|
* get log desc
|
|
133
130
|
* 描述字符串作为最后参数调用,显示时,前置
|
|
134
|
-
* @param {*[]} args
|
|
131
|
+
* @param {*[]} args
|
|
135
132
|
* @returns {{desc: string, arg: *[]}}
|
|
136
133
|
*/
|
|
137
134
|
function getDesc(args) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
135
|
+
let desc = '';
|
|
136
|
+
const last = args.at(-1);
|
|
137
|
+
if (typeof last === 'string') {
|
|
138
|
+
desc = last;
|
|
139
|
+
args.pop();
|
|
140
|
+
}
|
|
141
|
+
return {desc, arg: args}
|
|
145
142
|
}
|
|
146
143
|
|
|
147
144
|
/**
|
|
@@ -151,77 +148,74 @@ function getDesc(args) {
|
|
|
151
148
|
* returns {*}
|
|
152
149
|
*/
|
|
153
150
|
function log(...args) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
151
|
+
const last = args.at(-1);
|
|
152
|
+
|
|
153
|
+
// 全局日志
|
|
154
|
+
if (args.length !== 1 || !last?.m) {
|
|
155
|
+
const desc = getDesc(args);
|
|
156
|
+
desc ? console.log(desc, ...args) : console.log(...args);
|
|
157
|
+
return // 退出,不创建实例
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
|
161
|
+
const lg = new Log(last?.m);
|
|
162
|
+
/** @param {*} args2 */
|
|
163
|
+
const R = (...args2) => lg.log(...args2);
|
|
164
|
+
R.debug = lg.debug.bind(lg);
|
|
165
|
+
R.info = lg.info.bind(lg);
|
|
166
|
+
R.warn = lg.warn.bind(lg);
|
|
167
|
+
R.info = lg.info.bind(lg);
|
|
168
|
+
R.trace = lg.trace.bind(lg);
|
|
169
|
+
R.error = lg.error.bind(lg);
|
|
170
|
+
R.err = lg.err.bind(lg);
|
|
171
|
+
|
|
172
|
+
return R
|
|
176
173
|
}
|
|
177
174
|
|
|
178
175
|
/**
|
|
179
176
|
* 用于 catch(e) log.err(e)
|
|
180
177
|
* @param {...any} args - params */
|
|
181
178
|
log.err = (...args) => {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
)
|
|
188
|
-
args[0] = { exp: args[0].message };
|
|
189
|
-
desc ? console.error(desc, ...arg) : console.error(...args);
|
|
179
|
+
const {desc, arg} = getDesc(args);
|
|
180
|
+
const first = args?.[0];
|
|
181
|
+
if (first instanceof Error || (first?.message && first?.cause && first?.stack))
|
|
182
|
+
args[0] = {exp: args[0].message};
|
|
183
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
|
190
184
|
};
|
|
191
185
|
|
|
192
186
|
/**
|
|
193
187
|
* @param {...any} args - params */
|
|
194
188
|
log.error = (...args) => {
|
|
195
|
-
|
|
196
|
-
|
|
189
|
+
const {desc, arg} = getDesc(args);
|
|
190
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
|
197
191
|
};
|
|
198
192
|
|
|
199
193
|
/**
|
|
200
194
|
* @param {...any} args - params */
|
|
201
195
|
log.warn = (...args) => {
|
|
202
|
-
|
|
203
|
-
|
|
196
|
+
const {desc, arg} = getDesc(args);
|
|
197
|
+
desc ? console.warn(desc, ...arg) : console.warn(...args);
|
|
204
198
|
};
|
|
205
199
|
|
|
206
200
|
/**
|
|
207
201
|
* @param {...any} args - params */
|
|
208
202
|
log.info = (...args) => {
|
|
209
|
-
|
|
210
|
-
|
|
203
|
+
const {desc, arg} = getDesc(args);
|
|
204
|
+
desc ? console.info(desc, ...arg) : console.info(...args);
|
|
211
205
|
};
|
|
212
206
|
|
|
213
207
|
/**
|
|
214
208
|
* @param {...any} args - params */
|
|
215
209
|
log.debug = (...args) => {
|
|
216
|
-
|
|
217
|
-
|
|
210
|
+
const {desc, arg} = getDesc(args);
|
|
211
|
+
desc ? console.log(desc, ...arg) : console.log(...args);
|
|
218
212
|
};
|
|
219
213
|
|
|
220
214
|
/**
|
|
221
215
|
* @param {...any} args - params */
|
|
222
216
|
log.trace = (...args) => {
|
|
223
|
-
|
|
224
|
-
|
|
217
|
+
const {desc, arg} = getDesc(args);
|
|
218
|
+
desc ? console.trace(desc, ...arg) : console.trace(...args);
|
|
225
219
|
};
|
|
226
220
|
|
|
227
221
|
exports.default = log;
|