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