@wiajs/log 4.3.15 → 4.3.16

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