@wiajs/log 4.3.13 → 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
@@ -1,191 +1,194 @@
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
  */
6
6
  (function (global, factory) {
7
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
8
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
9
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Log = {}));
10
- })(this, (function (exports) { 'use strict';
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
+ })(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 {string}
29
- */ getDesc(args) {
30
- let R = "";
31
- try {
32
- const _ = this;
33
- const { m } = _;
34
- let fn = "", desc = "";
35
- if (args.length > 1) {
36
- const last = args.at(-1);
37
- if (typeof last === "object") {
38
- ({ desc, fn } = last);
39
- } else if (typeof last === "string") desc = last;
40
- if (desc || fn) {
41
- fn = fn || _.fn;
42
- _.fn = fn;
43
- args.pop();
44
- }
45
- }
46
- fn = fn || _.fn;
47
- if (m) desc = `${desc}[${m}${fn ? ":" + fn : ""}]`; // eslint-disable-line
48
- R = desc;
49
- } catch (e) {
50
- console.error(`getDesc exp:${e.message}`);
51
- }
52
- return R;
53
- }
54
- /** @param {...any} args - params */ log(...args) {
55
- const _ = this;
56
- const last = args.at(-1);
57
- // clear fn
58
- if (args.length === 1 && typeof last === "object" && last.fn) _.fn = "";
59
- else {
60
- const desc = _.getDesc(args);
61
- console.log(desc, ...args);
62
- }
63
- }
64
- /** @param {...any} args - params */ debug(...args) {
65
- const _ = this;
66
- const desc = _.getDesc(args);
67
- if (desc) console.log(desc, ...args);
68
- else console.log(...args);
69
- }
70
- /** @param {...any} args - params */ info(...args) {
71
- const _ = this;
72
- const desc = _.getDesc(args);
73
- if (desc) console.info(desc, ...args);
74
- else console.log(...args);
75
- }
76
- /** @param {...any} args - params */ warn(...args) {
77
- const _ = this;
78
- const { desc, arg } = _.getDesc(args);
79
- if (desc) console.warn(desc, ...arg);
80
- else console.log(...args);
81
- }
82
- /** @param {...any} args - params */ trace(...args) {
83
- const _ = this;
84
- const { desc, arg } = _.getDesc(args);
85
- if (desc) console.trace(desc, ...arg);
86
- else console.trace(...args);
87
- }
88
- /** @param {...any} args - params */ error(...args) {
89
- const _ = this;
90
- const desc = _.getDesc(args);
91
- if (desc) console.error(desc, ...args);
92
- else console.log(...args);
93
- }
94
- /**
95
- * 用于 catch(e) log.err(e)
96
- * @param {...any} args - params */ err(...args) {
97
- const _ = this;
98
- const first = args?.[0];
99
- if (first instanceof Error || first && first.message && first.cause && first.stack) args[0] = {
100
- exp: args[0].message
101
- };
102
- _.error(...args);
103
- }
104
- };
105
- function getDesc(args) {
106
- let desc = "";
107
- const last = args.at(-1);
108
- if (typeof last === "string") {
109
- desc = last;
110
- args.pop();
111
- }
112
- return desc;
113
- }
114
- /**
115
- * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
116
- * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
117
- * @param {...any} args - params
118
- * returns {*}
119
- */ function log(...args) {
120
- const last = args.at(-1);
121
- // 全局日志
122
- if (args.length !== 1 || !last?.m) {
123
- const desc = getDesc(args);
124
- desc ? console.log(desc, ...args) : console.log(...args);
125
- return;
126
- }
127
- // 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
128
- const lg = new Log(last?.m);
129
- /** @param {*} args2 */ const R = (...args2)=>lg.log(...args2);
130
- R.debug = lg.debug.bind(lg);
131
- R.info = lg.info.bind(lg);
132
- R.warn = lg.warn.bind(lg);
133
- R.info = lg.info.bind(lg);
134
- R.trace = lg.trace.bind(lg);
135
- R.error = lg.error.bind(lg);
136
- R.err = lg.err.bind(lg);
137
- return R;
138
- }
139
- /**
140
- * 用于 catch(e) log.err(e)
141
- * @param {...any} args - params */ log.err = (...args)=>{
142
- const desc = getDesc(args);
143
- const first = args?.[0];
144
- if (first instanceof Error || first && first.message && first.cause && first.stack) args[0] = {
145
- exp: args[0].message
146
- };
147
- desc ? console.error(desc, ...args) : console.error(...args);
148
- };
149
- /**
150
- * @param {...any} args - params */ log.error = (...args)=>{
151
- const desc = getDesc(args);
152
- desc ? console.error(desc, ...args) : console.error(...args);
153
- };
154
- /**
155
- * @param {...any} args - params */ log.warn = (...args)=>{
156
- const desc = getDesc(args);
157
- desc ? console.warn(desc, ...args) : console.warn(...args);
158
- };
159
- /**
160
- * @param {...any} args - params */ log.info = (...args)=>{
161
- const desc = getDesc(args);
162
- desc ? console.info(desc, ...args) : console.info(...args);
163
- };
164
- /**
165
- * @param {...any} args - params */ log.debug = (...args)=>{
166
- const desc = getDesc(args);
167
- desc ? console.log(desc, ...args) : console.log(...args);
168
- };
169
- /**
170
- * @param {...any} args - params */ log.trace = (...args)=>{
171
- const desc = getDesc(args);
172
- desc ? console.trace(desc, ...args) : console.trace(...args);
173
- };
174
- /**
175
- * 获取模块文件名称
176
- * esm: import.meta.url or cjs: __filename
177
- * @param {string} file
178
- * @returns
179
- */ function name(file) {
180
- // import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
181
- // file = fileURLToPath(file) // fileUrl 转成路径
182
- // const baseName = path.basename(file)
183
- // return baseName.replace(path.extname(baseName), '')
184
- return '';
185
- }
186
- // export { log, Log };
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
+ };
187
191
 
188
- exports.log = log;
189
- exports.name = name;
192
+ return log;
190
193
 
191
194
  }));
package/dist/log.min.js CHANGED
@@ -1,6 +1,6 @@
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
  */
6
- var o,e;o=this,e=function(o){"use strict";let e=class{constructor(o){this.m="",this.fn="",this.m=o}getDesc(o){let e="";try{const n=this,{m:s}=n;let t="",c="";if(o.length>1){const e=o.at(-1);"object"==typeof e?({desc:c,fn:t}=e):"string"==typeof e&&(c=e),(c||t)&&(t=t||n.fn,n.fn=t,o.pop())}t=t||n.fn,s&&(c=`${c}[${s}${t?":"+t:""}]`),e=c}catch(o){console.error(`getDesc exp:${o.message}`)}return e}log(...o){const e=this,n=o.at(-1);if(1===o.length&&"object"==typeof n&&n.fn)e.fn="";else{const n=e.getDesc(o);console.log(n,...o)}}debug(...o){const e=this.getDesc(o);e?console.log(e,...o):console.log(...o)}info(...o){const e=this.getDesc(o);e?console.info(e,...o):console.log(...o)}warn(...o){const{desc:e,arg:n}=this.getDesc(o);e?console.warn(e,...n):console.log(...o)}trace(...o){const{desc:e,arg:n}=this.getDesc(o);e?console.trace(e,...n):console.trace(...o)}error(...o){const e=this.getDesc(o);e?console.error(e,...o):console.log(...o)}err(...o){const e=o?.[0];(e instanceof Error||e&&e.message&&e.cause&&e.stack)&&(o[0]={exp:o[0].message}),this.error(...o)}};function n(o){let e="";const n=o.at(-1);return"string"==typeof n&&(e=n,o.pop()),e}function s(...o){const s=o.at(-1);if(1!==o.length||!s?.m){const e=n(o);return void(e?console.log(e,...o):console.log(...o))}const t=new e(s?.m),c=(...o)=>t.log(...o);return c.debug=t.debug.bind(t),c.info=t.info.bind(t),c.warn=t.warn.bind(t),c.info=t.info.bind(t),c.trace=t.trace.bind(t),c.error=t.error.bind(t),c.err=t.err.bind(t),c}s.err=(...o)=>{const e=n(o),s=o?.[0];(s instanceof Error||s&&s.message&&s.cause&&s.stack)&&(o[0]={exp:o[0].message}),e?console.error(e,...o):console.error(...o)},s.error=(...o)=>{const e=n(o);e?console.error(e,...o):console.error(...o)},s.warn=(...o)=>{const e=n(o);e?console.warn(e,...o):console.warn(...o)},s.info=(...o)=>{const e=n(o);e?console.info(e,...o):console.info(...o)},s.debug=(...o)=>{const e=n(o);e?console.log(e,...o):console.log(...o)},s.trace=(...o)=>{const e=n(o);e?console.trace(e,...o):console.trace(...o)},o.log=s,o.name=function(o){return""}},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((o="undefined"!=typeof globalThis?globalThis:o||self).Log={});
6
+ var e,o;e=this,o=function(){"use strict";let e=class{constructor(e){this.m="",this.fn="",this.m=e}getDesc(e){let o={desc:"",arg:e};try{const s=this,{m:n}=s;let c="",r="";if(e.length>1){const o=e.at(-1);"object"==typeof o?({desc:r,fn:c}=o):"string"==typeof o&&(r=o),(r||c)&&(c=c||s.fn,s.fn=c,e.pop())}c=c||s.fn,n&&(r=`${r}[${n}${c?`:${c}`:""}]`),o={desc:r,arg:e}}catch(e){console.error(`getDesc exp:${e.message}`)}return o}log(...e){const o=this,s=e.at(-1);if(1===e.length&&"object"==typeof s&&s.fn)o.fn="";else{const s=o.getDesc(e);console.log(s,...e)}}debug(...e){const o=this.getDesc(e);o?console.log(o,...e):console.log(...e)}info(...e){const o=this.getDesc(e);o?console.info(o,...e):console.log(...e)}warn(...e){const{desc:o,arg:s}=this.getDesc(e);o?console.warn(o,...s):console.log(...e)}trace(...e){const{desc:o,arg:s}=this.getDesc(e);o?console.trace(o,...s):console.trace(...e)}error(...e){const{desc:o,arg:s}=this.getDesc(e);o?console.error(o,...s):console.log(...e)}err(...e){const o=e?.[0];(o instanceof Error||o?.message&&o?.cause&&o?.stack)&&(e[0]={exp:e[0].message}),this.error(...e)}};function o(e){let o="";const s=e.at(-1);return"string"==typeof s&&(o=s,e.pop()),{desc:o,arg:e}}function s(...s){const n=s.at(-1);if(1!==s.length||!n?.m){const e=o(s);return void(e?console.log(e,...s):console.log(...s))}const c=new e(n?.m),r=(...e)=>c.log(...e);return r.debug=c.debug.bind(c),r.info=c.info.bind(c),r.warn=c.warn.bind(c),r.info=c.info.bind(c),r.trace=c.trace.bind(c),r.error=c.error.bind(c),r.err=c.err.bind(c),r}return s.err=(...e)=>{const{desc:s,arg:n}=o(e),c=e?.[0];(c instanceof Error||c?.message&&c?.cause&&c?.stack)&&(e[0]={exp:e[0].message}),s?console.error(s,...n):console.error(...e)},s.error=(...e)=>{const{desc:s,arg:n}=o(e);s?console.error(s,...n):console.error(...e)},s.warn=(...e)=>{const{desc:s,arg:n}=o(e);s?console.warn(s,...n):console.warn(...e)},s.info=(...e)=>{const{desc:s,arg:n}=o(e);s?console.info(s,...n):console.info(...e)},s.debug=(...e)=>{const{desc:s,arg:n}=o(e);s?console.log(s,...n):console.log(...e)},s.trace=(...e)=>{const{desc:s,arg:n}=o(e);s?console.trace(s,...n):console.trace(...e)},s},"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).Log=o();