@wiajs/log 4.3.19 → 4.3.20

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.
package/dist/log.cjs CHANGED
@@ -1,166 +1,135 @@
1
1
  /*!
2
- * wia log v4.3.19
3
- * (c) 2024-2024 Sibyl Yu and contributors
2
+ * wia log v4.3.20
3
+ * (c) 2024-2025 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
6
6
  'use strict';
7
7
 
8
8
  Object.defineProperty(exports, '__esModule', { value: true });
9
9
 
10
- const path = require('node:path');
11
- const node_url = require('node:url');
12
10
  const debug = require('debug');
11
+ const path = require('path');
12
+ const url = require('url');
13
13
 
14
- /**
15
- * debug日志封装,
16
- * 使用方法:
17
- * import {log as Log, filename} from './log.js'
18
- * const log = Log({env: `wia:${filename(__filename)}`})
19
- * log('hello')
20
- * log({a: 1, b: 2}, 'fun')
21
- * log.error/err/info/warn
22
- * $env:DEBUG=* 全开
23
- * $env:DEBUG=*:log 全开
24
- * $env:DEBUG=*:info log 不开,其他全开
25
- * warn 和 err 一直打开!
26
- */
27
- class Log {
28
- /**
29
- * 构造函数
30
- * @param {*} opts {
31
- * pre: 前缀,一般是模块名称,
32
- * env: NODE_DEBUG 环境变量
33
- * }
34
- */
35
- constructor(opts) {
36
- let {env} = opts;
37
- env = env ?? '';
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
-
44
- // 仅最后调用生效,覆盖环境变量
45
- if (dgs.debug.enabled) debug.enable('*');
46
- else if (dgs.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
47
- else debug.enable(`${env}:err,${env}:warn`);
48
-
49
- this.dgs = dgs;
50
- }
51
-
52
- /**
53
- *
54
- * @param {...any} args
55
- */
56
- debug(...args) {
57
- const first = args?.at(0);
58
- const last = args?.at(-1);
59
- if (typeof first === 'string') {
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
- // args[0] = `${this.pre}:${args[0]}`
64
- // console.debug(...args)
65
- // console.debug(this.pre, ...args)
66
- }
67
-
68
- /**
69
- *
70
- * @param {...any} args
71
- */
72
- error(...args) {
73
- const first = args?.at(0);
74
- const last = args?.at(-1);
75
- if (typeof first === 'string') {
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
- }
80
-
81
- /**
82
- *
83
- * @param {...any} args
84
- */
85
- err(...args) {
86
- const first = args?.[0];
87
- if (first?.message || first?.msg) {
88
- args[0] = {exp: first.message || first.msg};
89
- if (first?.code) args[0].exp += ` code:${first.code}`;
90
- }
91
- this.error(...args);
92
- }
93
-
94
- /**
95
- *
96
- * @param {...any} args
97
- */
98
- warn(...args) {
99
- const first = args?.at(0);
100
- const last = args?.at(-1);
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);
106
- }
107
-
108
- /**
109
- *
110
- * @param {...any} args
111
- */
112
- info(...args) {
113
- const first = args?.at(0);
114
- const last = args?.at(-1);
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);
122
- }
123
- }
124
-
125
- /**
126
- * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
127
- * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
128
- * @param {...any} args - params
129
- * returns {pino & (...args) => void}
130
- */
131
- function log(...args) {
132
- const last = args.at(-1);
133
-
134
- // 全局日志
135
- if (args.length !== 1 || !last?.env) return
136
-
137
- const {env} = last;
138
- // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
139
- const lg = new Log({env});
140
-
141
- /** @param {*} args2 */
142
- const R = (...args2) => lg.debug(...args2);
143
- R.debug = lg.debug.bind(lg);
144
- R.info = lg.info.bind(lg);
145
- R.warn = lg.warn.bind(lg);
146
- R.info = lg.info.bind(lg);
147
- R.error = lg.error.bind(lg);
148
- R.err = lg.err.bind(lg);
149
-
150
- return R
151
- }
152
-
153
- /**
154
- * 获取模块文件名称
155
- * esm: import.meta.url or cjs: __filename
156
- * @param {string} file
157
- * @returns
158
- */
159
- function name(file) {
160
- // import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
161
- file = node_url.fileURLToPath(file); // fileUrl 转成路径
162
- const baseName = path.basename(file);
163
- return baseName.replace(path.extname(baseName), '')
14
+ let Log = class Log {
15
+ /**
16
+ *
17
+ * @param {...any} args
18
+ */ debug(...args) {
19
+ const first = args == null ? void 0 : args.at(0);
20
+ const last = args == null ? void 0 : args.at(-1);
21
+ if (typeof first === 'string') {
22
+ args.shift();
23
+ this.dgs.debug(first, ...args);
24
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.debug(`${last}:%O`, first);
25
+ // args[0] = `${this.pre}:${args[0]}`
26
+ // console.debug(...args)
27
+ // console.debug(this.pre, ...args)
28
+ }
29
+ /**
30
+ *
31
+ * @param {...any} args
32
+ */ error(...args) {
33
+ const first = args == null ? void 0 : args.at(0);
34
+ const last = args == null ? void 0 : args.at(-1);
35
+ if (typeof first === 'string') {
36
+ args.shift();
37
+ this.dgs.err(` ${first}`, ...args);
38
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.err(` ${last}:%O`, first);
39
+ }
40
+ /**
41
+ *
42
+ * @param {...any} args
43
+ */ err(...args) {
44
+ const first = args == null ? void 0 : args[0];
45
+ if ((first == null ? void 0 : first.message) || (first == null ? void 0 : first.msg)) {
46
+ args[0] = {
47
+ exp: first.message || first.msg
48
+ };
49
+ if (first == null ? void 0 : first.code) args[0].exp += ` code:${first.code}`;
50
+ }
51
+ this.error(...args);
52
+ }
53
+ /**
54
+ *
55
+ * @param {...any} args
56
+ */ warn(...args) {
57
+ const first = args == null ? void 0 : args.at(0);
58
+ const last = args == null ? void 0 : args.at(-1);
59
+ if (typeof first === 'string') {
60
+ args.shift();
61
+ this.dgs.warn(first, ...args);
62
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.warn(`${last}:%O`, first);
63
+ }
64
+ /**
65
+ *
66
+ * @param {...any} args
67
+ */ info(...args) {
68
+ const first = args == null ? void 0 : args.at(0);
69
+ const last = args == null ? void 0 : args.at(-1);
70
+ // const dg = debug('ab')
71
+ // dg()
72
+ if (typeof first === 'string') {
73
+ args.shift();
74
+ this.dgs.info(first, ...args);
75
+ } else if (typeof first === 'object' && typeof last === 'string') this.dgs.info(`${last}:%O`, first);
76
+ }
77
+ /**
78
+ * 构造函数
79
+ * @param {*} opts {
80
+ * pre: 前缀,一般是模块名称,
81
+ * env: NODE_DEBUG 环境变量
82
+ * }
83
+ */ constructor(opts){
84
+ let { env } = opts;
85
+ env = env != null ? env : '';
86
+ const dgs = {} // debugs
87
+ ;
88
+ dgs.debug = debug(`${env}`);
89
+ dgs.info = debug(`${env}:info`);
90
+ dgs.err = debug(`${env}:err`);
91
+ dgs.warn = debug(`${env}:warn`);
92
+ // 仅最后调用生效,覆盖环境变量
93
+ if (dgs.debug.enabled) debug.enable('*');
94
+ else if (dgs.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
95
+ else debug.enable(`${env}:err,${env}:warn`);
96
+ this.dgs = dgs;
97
+ }
98
+ };
99
+ /**
100
+ * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
101
+ * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
102
+ * @param {...any} args - params
103
+ * returns {pino & (...args) => void}
104
+ */ function log(...args) {
105
+ const last = args.at(-1);
106
+ // 全局日志
107
+ if (args.length !== 1 || !(last == null ? void 0 : last.env)) return;
108
+ const { env } = last;
109
+ // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
110
+ const lg = new Log({
111
+ env
112
+ });
113
+ /** @param {*} args2 */ const R = (...args2)=>lg.debug(...args2);
114
+ R.debug = lg.debug.bind(lg);
115
+ R.info = lg.info.bind(lg);
116
+ R.warn = lg.warn.bind(lg);
117
+ R.info = lg.info.bind(lg);
118
+ R.error = lg.error.bind(lg);
119
+ R.err = lg.err.bind(lg);
120
+ return R;
121
+ }
122
+ /**
123
+ * 获取模块文件名称
124
+ * esm: import.meta.url or cjs: __filename
125
+ * @param {string} file
126
+ * @returns
127
+ */ function name(file) {
128
+ // import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
129
+ file = url.fileURLToPath(file) // fileUrl 转成路径
130
+ ;
131
+ const baseName = path.basename(file);
132
+ return baseName.replace(path.extname(baseName), '');
164
133
  }
165
134
 
166
135
  exports.default = Log;
package/dist/log.mjs CHANGED
@@ -1,217 +1,176 @@
1
1
  /*!
2
- * wia log v4.3.19
3
- * (c) 2024-2024 Sibyl Yu and contributors
2
+ * wia log v4.3.20
3
+ * (c) 2024-2025 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
6
6
  /**
7
7
  * 前端日志输出,封装 console日志,简化代码,支持模块或直接输出
8
8
  * 调用时,描述字符串后置,便于可选缺省,输出时,自带前置,类似 后端pino,保持前后端一致性
9
9
  * m 为模块,fn 为函数名称
10
- */
11
- class Log {
12
- /** @type {string} 模块 */
13
- m = ''
14
-
15
- /** @type {string} 函数 */
16
- fn = ''
17
-
18
- /**
19
- * @param {string} m 模块
20
- */
21
- constructor(m) {
22
- this.m = m;
23
- }
24
-
25
- /**
10
+ */ let Log = class Log {
11
+ /**
26
12
  * get log desc
27
- * 描述字符串作为最后参数调用,显示时,前置
13
+ * 描述字符串后置调用,前置显示
28
14
  * @param {*[]} args
29
- * @returns {{desc: string, arg: *[]}}
30
- */
31
- getDesc(args) {
32
- let R = {desc: '', arg: args};
33
- try {
34
- const _ = this;
35
- const {m} = _;
36
- let fn = '';
37
- let desc = '';
38
-
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 = {desc, arg: args};
53
- } catch (e) {
54
- console.error(`getDesc exp:${e.message}`);
55
- }
56
-
57
- return R
58
- }
59
-
60
- /** @param {...any} args - params */
61
- 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
-
72
- /** @param {...any} args - params */
73
- debug(...args) {
74
- const _ = this;
75
- const desc = _.getDesc(args);
76
- if (desc) console.log(desc, ...args);
77
- else console.log(...args);
78
- }
79
-
80
- /** @param {...any} args - params */
81
- info(...args) {
82
- const _ = this;
83
- const desc = _.getDesc(args);
84
- if (desc) console.info(desc, ...args);
85
- else console.log(...args);
86
- }
87
-
88
- /** @param {...any} args - params */
89
- warn(...args) {
90
- const _ = this;
91
- const {desc, arg} = _.getDesc(args);
92
- if (desc) console.warn(desc, ...arg);
93
- else console.log(...args);
94
- }
95
-
96
- /** @param {...any} args - params */
97
- trace(...args) {
98
- const _ = this;
99
- const {desc, arg} = _.getDesc(args);
100
- if (desc) console.trace(desc, ...arg);
101
- else console.trace(...args);
102
- }
103
-
104
- /** @param {...any} args - params */
105
- error(...args) {
106
- const _ = this;
107
- const {desc, arg} = _.getDesc(args);
108
- if (desc) console.error(desc, ...arg);
109
- else console.log(...args);
110
- }
111
-
112
- /**
15
+ * @returns {string}
16
+ */ getDesc(args) {
17
+ let R = '';
18
+ try {
19
+ const _ = this;
20
+ const { m } = _;
21
+ let fn = '', desc = '';
22
+ if (args.length > 1) {
23
+ const last = args.at(-1);
24
+ if (typeof last === 'object') {
25
+ ;
26
+ ({ desc, fn } = last);
27
+ } else if (typeof last === 'string') desc = last;
28
+ if (desc || fn) {
29
+ fn = fn || _.fn;
30
+ _.fn = fn;
31
+ args.pop();
32
+ }
33
+ }
34
+ fn = fn || _.fn;
35
+ if (m) desc = `${desc}[${m}${fn ? ':' + fn : ''}]` // eslint-disable-line
36
+ ;
37
+ R = desc;
38
+ } catch (e) {
39
+ console.error(`getDesc exp:${e.message}`);
40
+ }
41
+ return R;
42
+ }
43
+ /** @param {...any} args - params */ log(...args) {
44
+ const _ = this;
45
+ const last = args.at(-1);
46
+ // clear fn
47
+ if (args.length === 1 && typeof last === 'object' && last.fn) _.fn = '';
48
+ else {
49
+ const desc = _.getDesc(args);
50
+ console.log(desc, ...args);
51
+ }
52
+ }
53
+ /** @param {...any} args - params */ debug(...args) {
54
+ const _ = this;
55
+ const desc = _.getDesc(args);
56
+ if (desc) console.log(desc, ...args);
57
+ else console.log(...args);
58
+ }
59
+ /** @param {...any} args - params */ info(...args) {
60
+ const _ = this;
61
+ const desc = _.getDesc(args);
62
+ if (desc) console.info(desc, ...args);
63
+ else console.log(...args);
64
+ }
65
+ /** @param {...any} args - params */ warn(...args) {
66
+ const _ = this;
67
+ const { desc, arg } = _.getDesc(args);
68
+ if (desc) console.warn(desc, ...arg);
69
+ else console.log(...args);
70
+ }
71
+ /** @param {...any} args - params */ trace(...args) {
72
+ const _ = this;
73
+ const { desc, arg } = _.getDesc(args);
74
+ if (desc) console.trace(desc, ...arg);
75
+ else console.trace(...args);
76
+ }
77
+ /** @param {...any} args - params */ error(...args) {
78
+ const _ = this;
79
+ const desc = _.getDesc(args);
80
+ if (desc) console.error(desc, ...args);
81
+ else console.log(...args);
82
+ }
83
+ /**
113
84
  * 用于 catch(e) log.err(e)
114
- * @param {...any} args - params */
115
- err(...args) {
116
- const _ = this;
117
- const first = args?.[0];
118
- if (first instanceof Error || (first?.message && first?.cause && first?.stack))
119
- args[0] = {exp: args[0].message};
120
- _.error(...args);
121
- }
122
- }
123
-
85
+ * @param {...any} args - params */ err(...args) {
86
+ const _ = this;
87
+ const first = args == null ? void 0 : args[0];
88
+ if (first instanceof Error || first && first.message && first.cause && first.stack) args[0] = {
89
+ exp: args[0].message
90
+ };
91
+ _.error(...args);
92
+ }
93
+ /**
94
+ * @param {string} m 模块
95
+ */ constructor(m){
96
+ /** @type {string} 模块 */ this.m = '';
97
+ /** @type {string} 函数 */ this.fn = '';
98
+ this.m = m;
99
+ }
100
+ };
124
101
  /**
125
102
  * get log desc
126
103
  * 描述字符串作为最后参数调用,显示时,前置
127
104
  * @param {*[]} args
128
105
  * @returns {{desc: string, arg: *[]}}
129
- */
130
- function getDesc(args) {
131
- let desc = '';
132
- const last = args.at(-1);
133
- if (typeof last === 'string') {
134
- desc = last;
135
- args.pop();
136
- }
137
- return {desc, arg: args}
138
- }
139
-
106
+ */ function getDesc(args) {
107
+ let desc = '';
108
+ const last = args.at(-1);
109
+ if (typeof last === 'string') {
110
+ desc = last;
111
+ args.pop();
112
+ }
113
+ return desc;
114
+ }
140
115
  /**
141
116
  * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
142
117
  * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
143
118
  * @param {...any} args - params
144
119
  * returns {*}
145
- */
146
- function log(...args) {
147
- const last = args.at(-1);
148
-
149
- // 全局日志
150
- if (args.length !== 1 || !last?.m) {
151
- const desc = getDesc(args);
152
- desc ? console.log(desc, ...args) : console.log(...args);
153
- return // 退出,不创建实例
154
- }
155
-
156
- // 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
157
- const lg = new Log(last?.m);
158
- /** @param {*} args2 */
159
- const R = (...args2) => lg.log(...args2);
160
- R.debug = lg.debug.bind(lg);
161
- R.info = lg.info.bind(lg);
162
- R.warn = lg.warn.bind(lg);
163
- R.info = lg.info.bind(lg);
164
- R.trace = lg.trace.bind(lg);
165
- R.error = lg.error.bind(lg);
166
- R.err = lg.err.bind(lg);
167
-
168
- return R
169
- }
170
-
120
+ */ function log(...args) {
121
+ const last = args.at(-1);
122
+ // 全局日志
123
+ if (args.length !== 1 || !(last == null ? void 0 : last.m)) {
124
+ const desc = getDesc(args);
125
+ desc ? console.log(desc, ...args) : console.log(...args);
126
+ return; // 退出,不创建实例
127
+ }
128
+ // 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
129
+ const lg = new Log(last == null ? void 0 : last.m);
130
+ /** @param {*} args2 */ const R = (...args2)=>lg.log(...args2);
131
+ R.debug = lg.debug.bind(lg);
132
+ R.info = lg.info.bind(lg);
133
+ R.warn = lg.warn.bind(lg);
134
+ R.info = lg.info.bind(lg);
135
+ R.trace = lg.trace.bind(lg);
136
+ R.error = lg.error.bind(lg);
137
+ R.err = lg.err.bind(lg);
138
+ return R;
139
+ }
171
140
  /**
172
141
  * 用于 catch(e) log.err(e)
173
- * @param {...any} args - params */
174
- log.err = (...args) => {
175
- const {desc, arg} = getDesc(args);
176
- const first = args?.[0];
177
- if (first instanceof Error || (first?.message && first?.cause && first?.stack))
178
- args[0] = {exp: args[0].message};
179
- desc ? console.error(desc, ...arg) : console.error(...args);
180
- };
181
-
142
+ * @param {...any} args - params */ log.err = (...args)=>{
143
+ const desc = getDesc(args);
144
+ const first = args == null ? void 0 : args[0];
145
+ if (first instanceof Error || first && first.message && first.cause && first.stack) args[0] = {
146
+ exp: args[0].message
147
+ };
148
+ desc ? console.error(desc, ...args) : console.error(...args);
149
+ };
182
150
  /**
183
- * @param {...any} args - params */
184
- log.error = (...args) => {
185
- const {desc, arg} = getDesc(args);
186
- desc ? console.error(desc, ...arg) : console.error(...args);
187
- };
188
-
151
+ * @param {...any} args - params */ log.error = (...args)=>{
152
+ const desc = getDesc(args);
153
+ desc ? console.error(desc, ...args) : console.error(...args);
154
+ };
189
155
  /**
190
- * @param {...any} args - params */
191
- log.warn = (...args) => {
192
- const {desc, arg} = getDesc(args);
193
- desc ? console.warn(desc, ...arg) : console.warn(...args);
194
- };
195
-
156
+ * @param {...any} args - params */ log.warn = (...args)=>{
157
+ const desc = getDesc(args);
158
+ desc ? console.warn(desc, ...args) : console.warn(...args);
159
+ };
196
160
  /**
197
- * @param {...any} args - params */
198
- log.info = (...args) => {
199
- const {desc, arg} = getDesc(args);
200
- desc ? console.info(desc, ...arg) : console.info(...args);
201
- };
202
-
161
+ * @param {...any} args - params */ log.info = (...args)=>{
162
+ const desc = getDesc(args);
163
+ desc ? console.info(desc, ...args) : console.info(...args);
164
+ };
203
165
  /**
204
- * @param {...any} args - params */
205
- log.debug = (...args) => {
206
- const {desc, arg} = getDesc(args);
207
- desc ? console.log(desc, ...arg) : console.log(...args);
208
- };
209
-
166
+ * @param {...any} args - params */ log.debug = (...args)=>{
167
+ const desc = getDesc(args);
168
+ desc ? console.log(desc, ...args) : console.log(...args);
169
+ };
210
170
  /**
211
- * @param {...any} args - params */
212
- log.trace = (...args) => {
213
- const {desc, arg} = getDesc(args);
214
- desc ? console.trace(desc, ...arg) : console.trace(...args);
171
+ * @param {...any} args - params */ log.trace = (...args)=>{
172
+ const desc = getDesc(args);
173
+ desc ? console.trace(desc, ...args) : console.trace(...args);
215
174
  };
216
175
 
217
- export { log as default };
176
+ export { Log, log };