@wiajs/log 4.3.13 → 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.
@@ -0,0 +1,221 @@
1
+ /*!
2
+ * wia log v4.3.15
3
+ * (c) 2024-2024 Sibyl Yu and contributors
4
+ * Released under the MIT License.
5
+ */
6
+ 'use strict';
7
+
8
+ Object.defineProperty(exports, '__esModule', { value: true });
9
+
10
+ /**
11
+ * 前端日志输出,封装 console日志,简化代码,支持模块或直接输出
12
+ * 调用时,描述字符串后置,便于可选缺省,输出时,自带前置,类似 后端pino,保持前后端一致性
13
+ * m 为模块,fn 为函数名称
14
+ */
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 (first instanceof Error || (first?.message && first?.cause && first?.stack))
123
+ args[0] = {exp: args[0].message};
124
+ _.error(...args);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * get log desc
130
+ * 描述字符串作为最后参数调用,显示时,前置
131
+ * @param {*[]} args
132
+ * @returns {{desc: string, arg: *[]}}
133
+ */
134
+ function getDesc(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}
142
+ }
143
+
144
+ /**
145
+ * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
146
+ * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
147
+ * @param {...any} args - params
148
+ * returns {*}
149
+ */
150
+ function log(...args) {
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
173
+ }
174
+
175
+ /**
176
+ * 用于 catch(e) log.err(e)
177
+ * @param {...any} args - params */
178
+ log.err = (...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);
184
+ };
185
+
186
+ /**
187
+ * @param {...any} args - params */
188
+ log.error = (...args) => {
189
+ const {desc, arg} = getDesc(args);
190
+ desc ? console.error(desc, ...arg) : console.error(...args);
191
+ };
192
+
193
+ /**
194
+ * @param {...any} args - params */
195
+ log.warn = (...args) => {
196
+ const {desc, arg} = getDesc(args);
197
+ desc ? console.warn(desc, ...arg) : console.warn(...args);
198
+ };
199
+
200
+ /**
201
+ * @param {...any} args - params */
202
+ log.info = (...args) => {
203
+ const {desc, arg} = getDesc(args);
204
+ desc ? console.info(desc, ...arg) : console.info(...args);
205
+ };
206
+
207
+ /**
208
+ * @param {...any} args - params */
209
+ log.debug = (...args) => {
210
+ const {desc, arg} = getDesc(args);
211
+ desc ? console.log(desc, ...arg) : console.log(...args);
212
+ };
213
+
214
+ /**
215
+ * @param {...any} args - params */
216
+ log.trace = (...args) => {
217
+ const {desc, arg} = getDesc(args);
218
+ desc ? console.trace(desc, ...arg) : console.trace(...args);
219
+ };
220
+
221
+ exports.default = log;
package/dist/log.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia log v4.3.13
2
+ * wia log v4.3.15
3
3
  * (c) 2024-2024 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
@@ -35,18 +35,18 @@ class Log {
35
35
  constructor(opts) {
36
36
  let {env} = opts;
37
37
  env = env ?? '';
38
- const ds = {}; // debugs
39
- ds.debug = debug(`${env}`);
40
- ds.info = debug(`${env}:info`);
41
- ds.err = debug(`${env}:err`);
42
- ds.warn = debug(`${env}:warn`);
38
+ const dgs = {}; // debugs
39
+ dgs.debug = debug(`${env}`);
40
+ dgs.info = debug(`${env}:info`);
41
+ dgs.err = debug(`${env}:err`);
42
+ dgs.warn = debug(`${env}:warn`);
43
43
 
44
44
  // 仅最后调用生效,覆盖环境变量
45
- if (ds.debug.enabled) debug.enable('*');
46
- else if (ds.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
45
+ if (dgs.debug.enabled) debug.enable('*');
46
+ else if (dgs.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
47
47
  else debug.enable(`${env}:err,${env}:warn`);
48
48
 
49
- this.ds = ds;
49
+ this.dgs = dgs;
50
50
  }
51
51
 
52
52
  /**
@@ -57,9 +57,9 @@ class Log {
57
57
  const first = args?.at(0);
58
58
  const last = args?.at(-1);
59
59
  if (typeof first === 'string') {
60
- args[0] = `${first}`;
61
- this.ds.debug(...args);
62
- } else if (typeof first === 'object' && typeof last === 'string') this.ds.debug(`${last}:%O`, first);
60
+ args.shift();
61
+ this.dgs.debug(first, ...args);
62
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.debug(`${last}:%O`, first);
63
63
  // args[0] = `${this.pre}:${args[0]}`
64
64
  // console.debug(...args)
65
65
  // console.debug(this.pre, ...args)
@@ -73,9 +73,9 @@ class Log {
73
73
  const first = args?.at(0);
74
74
  const last = args?.at(-1);
75
75
  if (typeof first === 'string') {
76
- args[0] = ` ${first}`;
77
- this.ds.err(...args);
78
- } else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
76
+ args.shift();
77
+ this.dgs.err(` ${first}`, ...args);
78
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.err(` ${last}:%O`, first);
79
79
  }
80
80
 
81
81
  /**
@@ -98,8 +98,11 @@ class Log {
98
98
  warn(...args) {
99
99
  const first = args?.at(0);
100
100
  const last = args?.at(-1);
101
- if (typeof first === 'string') this.ds.warn(...args);
102
- else if (typeof first === 'object' && typeof last === 'string') this.ds.warn(`${last}:%O`, first);
101
+ if (typeof first === 'string') {
102
+ args.shift();
103
+ this.dgs.warn(first, ...args);
104
+ }
105
+ else if (typeof first === 'object' && typeof last === 'string') this.dgs.warn(`${last}:%O`, first);
103
106
  }
104
107
 
105
108
  /**
@@ -107,10 +110,15 @@ class Log {
107
110
  * @param {...any} args
108
111
  */
109
112
  info(...args) {
110
- const first = args?.at(0);
113
+ const first = args?.at(0);
111
114
  const last = args?.at(-1);
112
- if (typeof first === 'string') this.ds.info(...args);
113
- else if (typeof first === 'object' && typeof last === 'string') this.ds.info(`${last}:%O`, first);
115
+ // const dg = debug('ab')
116
+ // dg()
117
+ if (typeof first === 'string') {
118
+ args.shift();
119
+ this.dgs.info(first, ...args);
120
+ }
121
+ else if (typeof first === 'object' && typeof last === 'string') this.dgs.info(`${last}:%O`, first);
114
122
  }
115
123
  }
116
124