@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 CHANGED
@@ -13,135 +13,132 @@ Object.defineProperty(exports, '__esModule', { value: true });
13
13
  * m 为模块,fn 为函数名称
14
14
  */
15
15
  class Log {
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 (
123
- first instanceof Error ||
124
- (first?.message && first?.cause && first?.stack)
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
- let desc = "";
139
- const last = args.at(-1);
140
- if (typeof last === "string") {
141
- desc = last;
142
- args.pop();
143
- }
144
- return {desc, arg: args};
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
- const last = args.at(-1);
155
-
156
- // 全局日志
157
- if (args.length !== 1 || !last?.m) {
158
- const desc = getDesc(args);
159
- desc ? console.log(desc, ...args) : console.log(...args);
160
- return; // 退出,不创建实例
161
- }
162
-
163
- // 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
164
- const lg = new Log(last?.m);
165
- /** @param {*} args2 */
166
- const R = (...args2) => lg.log(...args2);
167
- R.debug = lg.debug.bind(lg);
168
- R.info = lg.info.bind(lg);
169
- R.warn = lg.warn.bind(lg);
170
- R.info = lg.info.bind(lg);
171
- R.trace = lg.trace.bind(lg);
172
- R.error = lg.error.bind(lg);
173
- R.err = lg.err.bind(lg);
174
-
175
- return R;
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
- const {desc, arg} = getDesc(args);
183
- const first = args?.[0];
184
- if (
185
- first instanceof Error ||
186
- (first?.message && first?.cause && first?.stack)
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
- const {desc, arg} = getDesc(args);
196
- desc ? console.error(desc, ...arg) : console.error(...args);
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
- const {desc, arg} = getDesc(args);
203
- desc ? console.warn(desc, ...arg) : console.warn(...args);
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
- const {desc, arg} = getDesc(args);
210
- desc ? console.info(desc, ...arg) : console.info(...args);
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
- const {desc, arg} = getDesc(args);
217
- desc ? console.log(desc, ...arg) : console.log(...args);
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
- const {desc, arg} = getDesc(args);
224
- desc ? console.trace(desc, ...arg) : console.trace(...args);
217
+ const {desc, arg} = getDesc(args);
218
+ desc ? console.trace(desc, ...arg) : console.trace(...args);
225
219
  };
226
220
 
227
221
  exports.default = log;