@wiajs/log 4.3.15 → 4.3.17
Sign up to get free protection for your applications and to get access to all the features.
- 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/types/browser.d.ts +18 -0
- package/types/node.d.ts +24 -0
package/dist/log.js
CHANGED
@@ -4,189 +4,191 @@
|
|
4
4
|
* Released under the MIT License.
|
5
5
|
*/
|
6
6
|
(function (global, factory) {
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
8
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
9
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Log = factory());
|
10
10
|
})(this, (function () { 'use strict';
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
12
|
+
/**
|
13
|
+
* 前端日志输出,封装 console日志,简化代码,支持模块或直接输出
|
14
|
+
* 调用时,描述字符串后置,便于可选缺省,输出时,自带前置,类似 后端pino,保持前后端一致性
|
15
|
+
* m 为模块,fn 为函数名称
|
16
|
+
*/ let Log = class Log {
|
17
|
+
/**
|
18
|
+
* @param {string} m 模块
|
19
|
+
*/ constructor(m){
|
20
|
+
/** @type {string} 模块 */ this.m = '';
|
21
|
+
/** @type {string} 函数 */ this.fn = '';
|
22
|
+
this.m = m;
|
23
|
+
}
|
24
|
+
/**
|
25
|
+
* get log desc
|
26
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
27
|
+
* @param {*[]} args
|
28
|
+
* @returns {{desc: string, arg: *[]}}
|
29
|
+
*/ getDesc(args) {
|
30
|
+
let R = {
|
31
|
+
desc: '',
|
32
|
+
arg: args
|
33
|
+
};
|
34
|
+
try {
|
35
|
+
const _ = this;
|
36
|
+
const { m } = _;
|
37
|
+
let fn = '';
|
38
|
+
let desc = '';
|
39
|
+
if (args.length > 1) {
|
40
|
+
const last = args.at(-1);
|
41
|
+
if (typeof last === 'object') {
|
42
|
+
;
|
43
|
+
({ desc, fn } = last);
|
44
|
+
} else if (typeof last === 'string') desc = last;
|
45
|
+
if (desc || fn) {
|
46
|
+
fn = fn || _.fn;
|
47
|
+
_.fn = fn;
|
48
|
+
args.pop();
|
49
|
+
}
|
50
|
+
}
|
51
|
+
fn = fn || _.fn;
|
52
|
+
if (m) desc = `${desc}[${m}${fn ? `:${fn}` : ''}]` // eslint-disable-line
|
53
|
+
;
|
54
|
+
R = {
|
55
|
+
desc,
|
56
|
+
arg: args
|
57
|
+
};
|
58
|
+
} catch (e) {
|
59
|
+
console.error(`getDesc exp:${e.message}`);
|
60
|
+
}
|
61
|
+
return R;
|
62
|
+
}
|
63
|
+
/** @param {...any} args - params */ log(...args) {
|
64
|
+
const _ = this;
|
65
|
+
const last = args.at(-1);
|
66
|
+
// clear fn
|
67
|
+
if (args.length === 1 && typeof last === 'object' && last.fn) _.fn = '';
|
68
|
+
else {
|
69
|
+
const desc = _.getDesc(args);
|
70
|
+
console.log(desc, ...args);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
/** @param {...any} args - params */ debug(...args) {
|
74
|
+
const _ = this;
|
75
|
+
const desc = _.getDesc(args);
|
76
|
+
if (desc) console.log(desc, ...args);
|
77
|
+
else console.log(...args);
|
78
|
+
}
|
79
|
+
/** @param {...any} args - params */ info(...args) {
|
80
|
+
const _ = this;
|
81
|
+
const desc = _.getDesc(args);
|
82
|
+
if (desc) console.info(desc, ...args);
|
83
|
+
else console.log(...args);
|
84
|
+
}
|
85
|
+
/** @param {...any} args - params */ warn(...args) {
|
86
|
+
const _ = this;
|
87
|
+
const { desc, arg } = _.getDesc(args);
|
88
|
+
if (desc) console.warn(desc, ...arg);
|
89
|
+
else console.log(...args);
|
90
|
+
}
|
91
|
+
/** @param {...any} args - params */ trace(...args) {
|
92
|
+
const _ = this;
|
93
|
+
const { desc, arg } = _.getDesc(args);
|
94
|
+
if (desc) console.trace(desc, ...arg);
|
95
|
+
else console.trace(...args);
|
96
|
+
}
|
97
|
+
/** @param {...any} args - params */ error(...args) {
|
98
|
+
const _ = this;
|
99
|
+
const { desc, arg } = _.getDesc(args);
|
100
|
+
if (desc) console.error(desc, ...arg);
|
101
|
+
else console.log(...args);
|
102
|
+
}
|
103
|
+
/**
|
104
|
+
* 用于 catch(e) log.err(e)
|
105
|
+
* @param {...any} args - params */ err(...args) {
|
106
|
+
const _ = this;
|
107
|
+
const first = args?.[0];
|
108
|
+
if (first instanceof Error || first?.message && first?.cause && first?.stack) args[0] = {
|
109
|
+
exp: args[0].message
|
110
|
+
};
|
111
|
+
_.error(...args);
|
112
|
+
}
|
113
|
+
};
|
114
|
+
/**
|
115
|
+
* get log desc
|
116
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
117
|
+
* @param {*[]} args
|
118
|
+
* @returns {{desc: string, arg: *[]}}
|
119
|
+
*/ function getDesc(args) {
|
120
|
+
let desc = '';
|
121
|
+
const last = args.at(-1);
|
122
|
+
if (typeof last === 'string') {
|
123
|
+
desc = last;
|
124
|
+
args.pop();
|
125
|
+
}
|
126
|
+
return {
|
127
|
+
desc,
|
128
|
+
arg: args
|
129
|
+
};
|
130
|
+
}
|
131
|
+
/**
|
132
|
+
* 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
|
133
|
+
* 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
|
134
|
+
* @param {...any} args - params
|
135
|
+
* returns {*}
|
136
|
+
*/ function log(...args) {
|
137
|
+
const last = args.at(-1);
|
138
|
+
// 全局日志
|
139
|
+
if (args.length !== 1 || !last?.m) {
|
140
|
+
const desc = getDesc(args);
|
141
|
+
desc ? console.log(desc, ...args) : console.log(...args);
|
142
|
+
return; // 退出,不创建实例
|
143
|
+
}
|
144
|
+
// 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
145
|
+
const lg = new Log(last?.m);
|
146
|
+
/** @param {*} args2 */ const R = (...args2)=>lg.log(...args2);
|
147
|
+
R.debug = lg.debug.bind(lg);
|
148
|
+
R.info = lg.info.bind(lg);
|
149
|
+
R.warn = lg.warn.bind(lg);
|
150
|
+
R.info = lg.info.bind(lg);
|
151
|
+
R.trace = lg.trace.bind(lg);
|
152
|
+
R.error = lg.error.bind(lg);
|
153
|
+
R.err = lg.err.bind(lg);
|
154
|
+
return R;
|
155
|
+
}
|
156
|
+
/**
|
157
|
+
* 用于 catch(e) log.err(e)
|
158
|
+
* @param {...any} args - params */ log.err = (...args)=>{
|
159
|
+
const { desc, arg } = getDesc(args);
|
160
|
+
const first = args?.[0];
|
161
|
+
if (first instanceof Error || first?.message && first?.cause && first?.stack) args[0] = {
|
162
|
+
exp: args[0].message
|
163
|
+
};
|
164
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
165
|
+
};
|
166
|
+
/**
|
167
|
+
* @param {...any} args - params */ log.error = (...args)=>{
|
168
|
+
const { desc, arg } = getDesc(args);
|
169
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
170
|
+
};
|
171
|
+
/**
|
172
|
+
* @param {...any} args - params */ log.warn = (...args)=>{
|
173
|
+
const { desc, arg } = getDesc(args);
|
174
|
+
desc ? console.warn(desc, ...arg) : console.warn(...args);
|
175
|
+
};
|
176
|
+
/**
|
177
|
+
* @param {...any} args - params */ log.info = (...args)=>{
|
178
|
+
const { desc, arg } = getDesc(args);
|
179
|
+
desc ? console.info(desc, ...arg) : console.info(...args);
|
180
|
+
};
|
181
|
+
/**
|
182
|
+
* @param {...any} args - params */ log.debug = (...args)=>{
|
183
|
+
const { desc, arg } = getDesc(args);
|
184
|
+
desc ? console.log(desc, ...arg) : console.log(...args);
|
185
|
+
};
|
186
|
+
/**
|
187
|
+
* @param {...any} args - params */ log.trace = (...args)=>{
|
188
|
+
const { desc, arg } = getDesc(args);
|
189
|
+
desc ? console.trace(desc, ...arg) : console.trace(...args);
|
190
|
+
};
|
189
191
|
|
190
|
-
|
192
|
+
return log;
|
191
193
|
|
192
194
|
}));
|