@wiajs/log 4.3.13 → 4.3.15
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/log.bs.cjs +227 -0
- package/dist/log.cjs +28 -20
- package/dist/log.js +46 -45
- package/dist/log.min.js +2 -2
- package/dist/log.mjs +201 -132
- package/package.json +9 -9
package/dist/log.bs.cjs
ADDED
@@ -0,0 +1,227 @@
|
|
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 (
|
123
|
+
first instanceof Error ||
|
124
|
+
(first?.message && first?.cause && first?.stack)
|
125
|
+
)
|
126
|
+
args[0] = { exp: args[0].message };
|
127
|
+
_.error(...args);
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
/**
|
132
|
+
* get log desc
|
133
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
134
|
+
* @param {*[]} args
|
135
|
+
* @returns {{desc: string, arg: *[]}}
|
136
|
+
*/
|
137
|
+
function getDesc(args) {
|
138
|
+
let desc = "";
|
139
|
+
const last = args.at(-1);
|
140
|
+
if (typeof last === "string") {
|
141
|
+
desc = last;
|
142
|
+
args.pop();
|
143
|
+
}
|
144
|
+
return {desc, arg: args};
|
145
|
+
}
|
146
|
+
|
147
|
+
/**
|
148
|
+
* 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
|
149
|
+
* 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
|
150
|
+
* @param {...any} args - params
|
151
|
+
* returns {*}
|
152
|
+
*/
|
153
|
+
function log(...args) {
|
154
|
+
const last = args.at(-1);
|
155
|
+
|
156
|
+
// 全局日志
|
157
|
+
if (args.length !== 1 || !last?.m) {
|
158
|
+
const desc = getDesc(args);
|
159
|
+
desc ? console.log(desc, ...args) : console.log(...args);
|
160
|
+
return; // 退出,不创建实例
|
161
|
+
}
|
162
|
+
|
163
|
+
// 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
164
|
+
const lg = new Log(last?.m);
|
165
|
+
/** @param {*} args2 */
|
166
|
+
const R = (...args2) => lg.log(...args2);
|
167
|
+
R.debug = lg.debug.bind(lg);
|
168
|
+
R.info = lg.info.bind(lg);
|
169
|
+
R.warn = lg.warn.bind(lg);
|
170
|
+
R.info = lg.info.bind(lg);
|
171
|
+
R.trace = lg.trace.bind(lg);
|
172
|
+
R.error = lg.error.bind(lg);
|
173
|
+
R.err = lg.err.bind(lg);
|
174
|
+
|
175
|
+
return R;
|
176
|
+
}
|
177
|
+
|
178
|
+
/**
|
179
|
+
* 用于 catch(e) log.err(e)
|
180
|
+
* @param {...any} args - params */
|
181
|
+
log.err = (...args) => {
|
182
|
+
const {desc, arg} = getDesc(args);
|
183
|
+
const first = args?.[0];
|
184
|
+
if (
|
185
|
+
first instanceof Error ||
|
186
|
+
(first?.message && first?.cause && first?.stack)
|
187
|
+
)
|
188
|
+
args[0] = { exp: args[0].message };
|
189
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
190
|
+
};
|
191
|
+
|
192
|
+
/**
|
193
|
+
* @param {...any} args - params */
|
194
|
+
log.error = (...args) => {
|
195
|
+
const {desc, arg} = getDesc(args);
|
196
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
197
|
+
};
|
198
|
+
|
199
|
+
/**
|
200
|
+
* @param {...any} args - params */
|
201
|
+
log.warn = (...args) => {
|
202
|
+
const {desc, arg} = getDesc(args);
|
203
|
+
desc ? console.warn(desc, ...arg) : console.warn(...args);
|
204
|
+
};
|
205
|
+
|
206
|
+
/**
|
207
|
+
* @param {...any} args - params */
|
208
|
+
log.info = (...args) => {
|
209
|
+
const {desc, arg} = getDesc(args);
|
210
|
+
desc ? console.info(desc, ...arg) : console.info(...args);
|
211
|
+
};
|
212
|
+
|
213
|
+
/**
|
214
|
+
* @param {...any} args - params */
|
215
|
+
log.debug = (...args) => {
|
216
|
+
const {desc, arg} = getDesc(args);
|
217
|
+
desc ? console.log(desc, ...arg) : console.log(...args);
|
218
|
+
};
|
219
|
+
|
220
|
+
/**
|
221
|
+
* @param {...any} args - params */
|
222
|
+
log.trace = (...args) => {
|
223
|
+
const {desc, arg} = getDesc(args);
|
224
|
+
desc ? console.trace(desc, ...arg) : console.trace(...args);
|
225
|
+
};
|
226
|
+
|
227
|
+
exports.default = log;
|
package/dist/log.cjs
CHANGED
@@ -1,5 +1,5 @@
|
|
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
|
*/
|
@@ -35,18 +35,18 @@ class Log {
|
|
35
35
|
constructor(opts) {
|
36
36
|
let {env} = opts;
|
37
37
|
env = env ?? '';
|
38
|
-
const
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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 (
|
46
|
-
else if (
|
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.
|
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
|
61
|
-
this.
|
62
|
-
} else if (typeof first === 'object' && typeof last === 'string') this.
|
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
|
77
|
-
this.
|
78
|
-
} else if (typeof first === 'object' && typeof last === 'string') this.
|
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')
|
102
|
-
|
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
|
-
|
113
|
-
|
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
|
|
package/dist/log.js
CHANGED
@@ -1,13 +1,13 @@
|
|
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
6
|
(function (global, factory) {
|
7
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(
|
8
|
-
typeof define === 'function' && define.amd ? define(
|
9
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self,
|
10
|
-
})(this, (function (
|
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
12
|
/**
|
13
13
|
* 前端日志输出,封装 console日志,简化代码,支持模块或直接输出
|
@@ -23,15 +23,19 @@
|
|
23
23
|
}
|
24
24
|
/**
|
25
25
|
* get log desc
|
26
|
-
*
|
26
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
27
27
|
* @param {*[]} args
|
28
|
-
* @returns {string}
|
28
|
+
* @returns {{desc: string, arg: *[]}}
|
29
29
|
*/ getDesc(args) {
|
30
|
-
let R =
|
30
|
+
let R = {
|
31
|
+
desc: '',
|
32
|
+
arg: args
|
33
|
+
};
|
31
34
|
try {
|
32
35
|
const _ = this;
|
33
36
|
const { m } = _;
|
34
|
-
let fn = ""
|
37
|
+
let fn = "";
|
38
|
+
let desc = "";
|
35
39
|
if (args.length > 1) {
|
36
40
|
const last = args.at(-1);
|
37
41
|
if (typeof last === "object") {
|
@@ -44,8 +48,11 @@
|
|
44
48
|
}
|
45
49
|
}
|
46
50
|
fn = fn || _.fn;
|
47
|
-
if (m) desc = `${desc}[${m}${fn ?
|
48
|
-
R =
|
51
|
+
if (m) desc = `${desc}[${m}${fn ? `:${fn}` : ""}]`; // eslint-disable-line
|
52
|
+
R = {
|
53
|
+
desc,
|
54
|
+
arg: args
|
55
|
+
};
|
49
56
|
} catch (e) {
|
50
57
|
console.error(`getDesc exp:${e.message}`);
|
51
58
|
}
|
@@ -87,8 +94,8 @@
|
|
87
94
|
}
|
88
95
|
/** @param {...any} args - params */ error(...args) {
|
89
96
|
const _ = this;
|
90
|
-
const desc = _.getDesc(args);
|
91
|
-
if (desc) console.error(desc, ...
|
97
|
+
const { desc, arg } = _.getDesc(args);
|
98
|
+
if (desc) console.error(desc, ...arg);
|
92
99
|
else console.log(...args);
|
93
100
|
}
|
94
101
|
/**
|
@@ -96,20 +103,28 @@
|
|
96
103
|
* @param {...any} args - params */ err(...args) {
|
97
104
|
const _ = this;
|
98
105
|
const first = args?.[0];
|
99
|
-
if (first instanceof Error || first
|
106
|
+
if (first instanceof Error || first?.message && first?.cause && first?.stack) args[0] = {
|
100
107
|
exp: args[0].message
|
101
108
|
};
|
102
109
|
_.error(...args);
|
103
110
|
}
|
104
111
|
};
|
105
|
-
|
112
|
+
/**
|
113
|
+
* get log desc
|
114
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
115
|
+
* @param {*[]} args
|
116
|
+
* @returns {{desc: string, arg: *[]}}
|
117
|
+
*/ function getDesc(args) {
|
106
118
|
let desc = "";
|
107
119
|
const last = args.at(-1);
|
108
120
|
if (typeof last === "string") {
|
109
121
|
desc = last;
|
110
122
|
args.pop();
|
111
123
|
}
|
112
|
-
return
|
124
|
+
return {
|
125
|
+
desc,
|
126
|
+
arg: args
|
127
|
+
};
|
113
128
|
}
|
114
129
|
/**
|
115
130
|
* 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
|
@@ -122,7 +137,7 @@
|
|
122
137
|
if (args.length !== 1 || !last?.m) {
|
123
138
|
const desc = getDesc(args);
|
124
139
|
desc ? console.log(desc, ...args) : console.log(...args);
|
125
|
-
return;
|
140
|
+
return; // 退出,不创建实例
|
126
141
|
}
|
127
142
|
// 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
128
143
|
const lg = new Log(last?.m);
|
@@ -139,53 +154,39 @@
|
|
139
154
|
/**
|
140
155
|
* 用于 catch(e) log.err(e)
|
141
156
|
* @param {...any} args - params */ log.err = (...args)=>{
|
142
|
-
const desc = getDesc(args);
|
157
|
+
const { desc, arg } = getDesc(args);
|
143
158
|
const first = args?.[0];
|
144
|
-
if (first instanceof Error || first
|
159
|
+
if (first instanceof Error || first?.message && first?.cause && first?.stack) args[0] = {
|
145
160
|
exp: args[0].message
|
146
161
|
};
|
147
|
-
desc ? console.error(desc, ...
|
162
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
148
163
|
};
|
149
164
|
/**
|
150
165
|
* @param {...any} args - params */ log.error = (...args)=>{
|
151
|
-
const desc = getDesc(args);
|
152
|
-
desc ? console.error(desc, ...
|
166
|
+
const { desc, arg } = getDesc(args);
|
167
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
153
168
|
};
|
154
169
|
/**
|
155
170
|
* @param {...any} args - params */ log.warn = (...args)=>{
|
156
|
-
const desc = getDesc(args);
|
157
|
-
desc ? console.warn(desc, ...
|
171
|
+
const { desc, arg } = getDesc(args);
|
172
|
+
desc ? console.warn(desc, ...arg) : console.warn(...args);
|
158
173
|
};
|
159
174
|
/**
|
160
175
|
* @param {...any} args - params */ log.info = (...args)=>{
|
161
|
-
const desc = getDesc(args);
|
162
|
-
desc ? console.info(desc, ...
|
176
|
+
const { desc, arg } = getDesc(args);
|
177
|
+
desc ? console.info(desc, ...arg) : console.info(...args);
|
163
178
|
};
|
164
179
|
/**
|
165
180
|
* @param {...any} args - params */ log.debug = (...args)=>{
|
166
|
-
const desc = getDesc(args);
|
167
|
-
desc ? console.log(desc, ...
|
181
|
+
const { desc, arg } = getDesc(args);
|
182
|
+
desc ? console.log(desc, ...arg) : console.log(...args);
|
168
183
|
};
|
169
184
|
/**
|
170
185
|
* @param {...any} args - params */ log.trace = (...args)=>{
|
171
|
-
const desc = getDesc(args);
|
172
|
-
desc ? console.trace(desc, ...
|
186
|
+
const { desc, arg } = getDesc(args);
|
187
|
+
desc ? console.trace(desc, ...arg) : console.trace(...args);
|
173
188
|
};
|
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 };
|
187
189
|
|
188
|
-
|
189
|
-
exports.name = name;
|
190
|
+
return log;
|
190
191
|
|
191
192
|
}));
|
package/dist/log.min.js
CHANGED
@@ -1,6 +1,6 @@
|
|
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
|
-
var o
|
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();
|
package/dist/log.mjs
CHANGED
@@ -1,154 +1,223 @@
|
|
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 {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
+
/**
|
26
|
+
* get log desc
|
27
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
28
|
+
* @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
|
+
/**
|
113
|
+
* 用于 catch(e) log.err(e)
|
114
|
+
* @param {...any} args - params */
|
115
|
+
err(...args) {
|
116
|
+
const _ = this;
|
117
|
+
const first = args?.[0];
|
118
|
+
if (
|
119
|
+
first instanceof Error ||
|
120
|
+
(first?.message && first?.cause && first?.stack)
|
121
|
+
)
|
122
|
+
args[0] = { exp: args[0].message };
|
123
|
+
_.error(...args);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
/**
|
128
|
+
* get log desc
|
129
|
+
* 描述字符串作为最后参数调用,显示时,前置
|
130
|
+
* @param {*[]} args
|
131
|
+
* @returns {{desc: string, arg: *[]}}
|
132
|
+
*/
|
133
|
+
function getDesc(args) {
|
134
|
+
let desc = "";
|
135
|
+
const last = args.at(-1);
|
136
|
+
if (typeof last === "string") {
|
137
|
+
desc = last;
|
138
|
+
args.pop();
|
139
|
+
}
|
140
|
+
return {desc, arg: args};
|
111
141
|
}
|
112
142
|
|
113
143
|
/**
|
114
144
|
* 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
|
115
145
|
* 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
|
116
146
|
* @param {...any} args - params
|
117
|
-
* returns {
|
147
|
+
* returns {*}
|
118
148
|
*/
|
119
149
|
function log(...args) {
|
120
|
-
|
150
|
+
const last = args.at(-1);
|
121
151
|
|
122
|
-
|
123
|
-
|
152
|
+
// 全局日志
|
153
|
+
if (args.length !== 1 || !last?.m) {
|
154
|
+
const desc = getDesc(args);
|
155
|
+
desc ? console.log(desc, ...args) : console.log(...args);
|
156
|
+
return; // 退出,不创建实例
|
157
|
+
}
|
124
158
|
|
125
|
-
|
126
|
-
|
127
|
-
|
159
|
+
// 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
160
|
+
const lg = new Log(last?.m);
|
161
|
+
/** @param {*} args2 */
|
162
|
+
const R = (...args2) => lg.log(...args2);
|
163
|
+
R.debug = lg.debug.bind(lg);
|
164
|
+
R.info = lg.info.bind(lg);
|
165
|
+
R.warn = lg.warn.bind(lg);
|
166
|
+
R.info = lg.info.bind(lg);
|
167
|
+
R.trace = lg.trace.bind(lg);
|
168
|
+
R.error = lg.error.bind(lg);
|
169
|
+
R.err = lg.err.bind(lg);
|
128
170
|
|
129
|
-
|
130
|
-
const R = (...args2) => lg.debug(...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.error = lg.error.bind(lg);
|
136
|
-
R.err = lg.err.bind(lg);
|
137
|
-
|
138
|
-
return R
|
171
|
+
return R;
|
139
172
|
}
|
140
173
|
|
141
174
|
/**
|
142
|
-
*
|
143
|
-
*
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
175
|
+
* 用于 catch(e) log.err(e)
|
176
|
+
* @param {...any} args - params */
|
177
|
+
log.err = (...args) => {
|
178
|
+
const {desc, arg} = getDesc(args);
|
179
|
+
const first = args?.[0];
|
180
|
+
if (
|
181
|
+
first instanceof Error ||
|
182
|
+
(first?.message && first?.cause && first?.stack)
|
183
|
+
)
|
184
|
+
args[0] = { exp: args[0].message };
|
185
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
186
|
+
};
|
187
|
+
|
188
|
+
/**
|
189
|
+
* @param {...any} args - params */
|
190
|
+
log.error = (...args) => {
|
191
|
+
const {desc, arg} = getDesc(args);
|
192
|
+
desc ? console.error(desc, ...arg) : console.error(...args);
|
193
|
+
};
|
194
|
+
|
195
|
+
/**
|
196
|
+
* @param {...any} args - params */
|
197
|
+
log.warn = (...args) => {
|
198
|
+
const {desc, arg} = getDesc(args);
|
199
|
+
desc ? console.warn(desc, ...arg) : console.warn(...args);
|
200
|
+
};
|
201
|
+
|
202
|
+
/**
|
203
|
+
* @param {...any} args - params */
|
204
|
+
log.info = (...args) => {
|
205
|
+
const {desc, arg} = getDesc(args);
|
206
|
+
desc ? console.info(desc, ...arg) : console.info(...args);
|
207
|
+
};
|
208
|
+
|
209
|
+
/**
|
210
|
+
* @param {...any} args - params */
|
211
|
+
log.debug = (...args) => {
|
212
|
+
const {desc, arg} = getDesc(args);
|
213
|
+
desc ? console.log(desc, ...arg) : console.log(...args);
|
214
|
+
};
|
215
|
+
|
216
|
+
/**
|
217
|
+
* @param {...any} args - params */
|
218
|
+
log.trace = (...args) => {
|
219
|
+
const {desc, arg} = getDesc(args);
|
220
|
+
desc ? console.trace(desc, ...arg) : console.trace(...args);
|
221
|
+
};
|
153
222
|
|
154
|
-
export {
|
223
|
+
export { log as default };
|
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.15",
|
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"
|