@wiajs/log 4.3.10 → 4.3.12
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 +103 -104
- package/dist/log.js +2 -2
- package/dist/log.min.js +2 -2
- package/dist/log.mjs +103 -104
- package/index.js +1 -1
- package/lib/browser.js +1 -2
- package/lib/node.js +41 -35
- package/package.json +1 -1
package/dist/log.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia log v4.3.
|
|
3
|
-
* (c)
|
|
2
|
+
* wia log v4.3.12
|
|
3
|
+
* (c) 2024-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
9
|
|
|
10
10
|
const path = require('node:path');
|
|
11
|
+
const node_url = require('node:url');
|
|
11
12
|
const debug = require('debug');
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -24,98 +25,93 @@ const debug = require('debug');
|
|
|
24
25
|
* warn 和 err 一直打开!
|
|
25
26
|
*/
|
|
26
27
|
class Log {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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 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`);
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
else debug.enable(`${env}:err,${env}:warn`);
|
|
44
|
+
// 仅最后调用生效,覆盖环境变量
|
|
45
|
+
if (ds.debug.enabled) debug.enable('*');
|
|
46
|
+
else if (ds.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
|
|
47
|
+
else debug.enable(`${env}:err,${env}:warn`);
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
this.ds = ds;
|
|
50
|
+
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
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[0] = `${first}`;
|
|
61
|
+
this.ds.debug(...args);
|
|
62
|
+
} else if (typeof first === 'object' && typeof last === 'string') this.ds.debug(`${last}:%O`, first);
|
|
63
|
+
// args[0] = `${this.pre}:${args[0]}`
|
|
64
|
+
// console.debug(...args)
|
|
65
|
+
// console.debug(this.pre, ...args)
|
|
66
|
+
}
|
|
68
67
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
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[0] = ` ${first}`;
|
|
77
|
+
this.ds.err(...args);
|
|
78
|
+
} else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
|
|
79
|
+
}
|
|
82
80
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
}
|
|
95
93
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
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') this.ds.warn(...args);
|
|
102
|
+
else if (typeof first === 'object' && typeof last === 'string') this.ds.warn(`${last}:%O`, first);
|
|
103
|
+
}
|
|
107
104
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
105
|
+
/**
|
|
106
|
+
*
|
|
107
|
+
* @param {...any} args
|
|
108
|
+
*/
|
|
109
|
+
info(...args) {
|
|
110
|
+
const first = args?.at(0);
|
|
111
|
+
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);
|
|
114
|
+
}
|
|
119
115
|
}
|
|
120
116
|
|
|
121
117
|
/**
|
|
@@ -125,35 +121,38 @@ class Log {
|
|
|
125
121
|
* returns {pino & (...args) => void}
|
|
126
122
|
*/
|
|
127
123
|
function log(...args) {
|
|
128
|
-
|
|
124
|
+
const last = args.at(-1);
|
|
129
125
|
|
|
130
|
-
|
|
131
|
-
|
|
126
|
+
// 全局日志
|
|
127
|
+
if (args.length !== 1 || !last?.env) return
|
|
132
128
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
const {env} = last;
|
|
130
|
+
// 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
|
131
|
+
const lg = new Log({env});
|
|
136
132
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
133
|
+
/** @param {*} args2 */
|
|
134
|
+
const R = (...args2) => lg.debug(...args2);
|
|
135
|
+
R.debug = lg.debug.bind(lg);
|
|
136
|
+
R.info = lg.info.bind(lg);
|
|
137
|
+
R.warn = lg.warn.bind(lg);
|
|
138
|
+
R.info = lg.info.bind(lg);
|
|
139
|
+
R.error = lg.error.bind(lg);
|
|
140
|
+
R.err = lg.err.bind(lg);
|
|
145
141
|
|
|
146
|
-
|
|
142
|
+
return R
|
|
147
143
|
}
|
|
148
144
|
|
|
149
145
|
/**
|
|
150
146
|
* 获取模块文件名称
|
|
147
|
+
* esm: import.meta.url or cjs: __filename
|
|
151
148
|
* @param {string} file
|
|
152
149
|
* @returns
|
|
153
150
|
*/
|
|
154
151
|
function name(file) {
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
// import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
|
|
153
|
+
file = node_url.fileURLToPath(file); // fileUrl 转成路径
|
|
154
|
+
const baseName = path.basename(file);
|
|
155
|
+
return baseName.replace(path.extname(baseName), '')
|
|
157
156
|
}
|
|
158
157
|
|
|
159
158
|
exports.default = Log;
|
package/dist/log.js
CHANGED
package/dist/log.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia log v4.3.
|
|
3
|
-
* (c)
|
|
2
|
+
* wia log v4.3.12
|
|
3
|
+
* (c) 2024-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
var o,e;o=this,e=function(){"use strict";let o=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 e(o){let e="";const n=o.at(-1);return"string"==typeof n&&(e=n,o.pop()),e}function n(...n){const s=n.at(-1);if(1!==n.length||!s?.m){const o=e(n);return void(o?console.log(o,...n):console.log(...n))}const t=new o(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}return n.err=(...o)=>{const n=e(o),s=o?.[0];(s instanceof Error||s&&s.message&&s.cause&&s.stack)&&(o[0]={exp:o[0].message}),n?console.error(n,...o):console.error(...o)},n.error=(...o)=>{const n=e(o);n?console.error(n,...o):console.error(...o)},n.warn=(...o)=>{const n=e(o);n?console.warn(n,...o):console.warn(...o)},n.info=(...o)=>{const n=e(o);n?console.info(n,...o):console.info(...o)},n.debug=(...o)=>{const n=e(o);n?console.log(n,...o):console.log(...o)},n.trace=(...o)=>{const n=e(o);n?console.trace(n,...o):console.trace(...o)},n},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(o="undefined"!=typeof globalThis?globalThis:o||self).Log=e();
|
package/dist/log.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia log v4.3.
|
|
3
|
-
* (c)
|
|
2
|
+
* wia log v4.3.12
|
|
3
|
+
* (c) 2024-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
7
8
|
import debug from 'debug';
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -20,98 +21,93 @@ import debug from 'debug';
|
|
|
20
21
|
* warn 和 err 一直打开!
|
|
21
22
|
*/
|
|
22
23
|
class Log {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
/**
|
|
25
|
+
* 构造函数
|
|
26
|
+
* @param {*} opts {
|
|
27
|
+
* pre: 前缀,一般是模块名称,
|
|
28
|
+
* env: NODE_DEBUG 环境变量
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
constructor(opts) {
|
|
32
|
+
let {env} = opts;
|
|
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`);
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else debug.enable(`${env}:err,${env}:warn`);
|
|
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
44
|
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
this.ds = ds;
|
|
46
|
+
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param {...any} args
|
|
51
|
+
*/
|
|
52
|
+
debug(...args) {
|
|
53
|
+
const first = args?.at(0);
|
|
54
|
+
const last = args?.at(-1);
|
|
55
|
+
if (typeof first === 'string') {
|
|
56
|
+
args[0] = `${first}`;
|
|
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)
|
|
62
|
+
}
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param {...any} args
|
|
67
|
+
*/
|
|
68
|
+
error(...args) {
|
|
69
|
+
const first = args?.at(0);
|
|
70
|
+
const last = args?.at(-1);
|
|
71
|
+
if (typeof first === 'string') {
|
|
72
|
+
args[0] = ` ${first}`;
|
|
73
|
+
this.ds.err(...args);
|
|
74
|
+
} else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
|
|
75
|
+
}
|
|
78
76
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param {...any} args
|
|
80
|
+
*/
|
|
81
|
+
err(...args) {
|
|
82
|
+
const first = args?.[0];
|
|
83
|
+
if (first?.message || first?.msg) {
|
|
84
|
+
args[0] = {exp: first.message || first.msg};
|
|
85
|
+
if (first?.code) args[0].exp += ` code:${first.code}`;
|
|
86
|
+
}
|
|
87
|
+
this.error(...args);
|
|
88
|
+
}
|
|
91
89
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
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);
|
|
99
|
+
}
|
|
103
100
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
101
|
+
/**
|
|
102
|
+
*
|
|
103
|
+
* @param {...any} args
|
|
104
|
+
*/
|
|
105
|
+
info(...args) {
|
|
106
|
+
const first = args?.at(0);
|
|
107
|
+
const last = args?.at(-1);
|
|
108
|
+
if (typeof first === 'string') this.ds.info(...args);
|
|
109
|
+
else if (typeof first === 'object' && typeof last === 'string') this.ds.info(`${last}:%O`, first);
|
|
110
|
+
}
|
|
115
111
|
}
|
|
116
112
|
|
|
117
113
|
/**
|
|
@@ -121,35 +117,38 @@ class Log {
|
|
|
121
117
|
* returns {pino & (...args) => void}
|
|
122
118
|
*/
|
|
123
119
|
function log(...args) {
|
|
124
|
-
|
|
120
|
+
const last = args.at(-1);
|
|
125
121
|
|
|
126
|
-
|
|
127
|
-
|
|
122
|
+
// 全局日志
|
|
123
|
+
if (args.length !== 1 || !last?.env) return
|
|
128
124
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
const {env} = last;
|
|
126
|
+
// 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
|
|
127
|
+
const lg = new Log({env});
|
|
132
128
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
129
|
+
/** @param {*} args2 */
|
|
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);
|
|
141
137
|
|
|
142
|
-
|
|
138
|
+
return R
|
|
143
139
|
}
|
|
144
140
|
|
|
145
141
|
/**
|
|
146
142
|
* 获取模块文件名称
|
|
143
|
+
* esm: import.meta.url or cjs: __filename
|
|
147
144
|
* @param {string} file
|
|
148
145
|
* @returns
|
|
149
146
|
*/
|
|
150
147
|
function name(file) {
|
|
151
|
-
|
|
152
|
-
|
|
148
|
+
// import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
|
|
149
|
+
file = fileURLToPath(file); // fileUrl 转成路径
|
|
150
|
+
const baseName = path.basename(file);
|
|
151
|
+
return baseName.replace(path.extname(baseName), '')
|
|
153
152
|
}
|
|
154
153
|
|
|
155
154
|
export { Log as default, log, name };
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {default, name, log} from './lib/node.js'
|
package/lib/browser.js
CHANGED
package/lib/node.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import path from
|
|
2
|
-
import
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import debug from 'debug';
|
|
3
4
|
/**
|
|
4
5
|
* debug日志封装,
|
|
5
6
|
* 使用方法:
|
|
@@ -14,54 +15,55 @@ import debug from "debug";
|
|
|
14
15
|
* warn 和 err 一直打开!
|
|
15
16
|
*/ export default class Log {
|
|
16
17
|
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
* 构造函数
|
|
19
|
+
* @param {*} opts {
|
|
20
|
+
* pre: 前缀,一般是模块名称,
|
|
21
|
+
* env: NODE_DEBUG 环境变量
|
|
22
|
+
* }
|
|
23
|
+
*/ constructor(opts){
|
|
23
24
|
let { env } = opts;
|
|
24
|
-
env = env ??
|
|
25
|
-
const ds = {}
|
|
25
|
+
env = env ?? '';
|
|
26
|
+
const ds = {} // debugs
|
|
27
|
+
;
|
|
26
28
|
ds.debug = debug(`${env}`);
|
|
27
29
|
ds.info = debug(`${env}:info`);
|
|
28
30
|
ds.err = debug(`${env}:err`);
|
|
29
31
|
ds.warn = debug(`${env}:warn`);
|
|
30
32
|
// 仅最后调用生效,覆盖环境变量
|
|
31
|
-
if (ds.debug.enabled) debug.enable(
|
|
33
|
+
if (ds.debug.enabled) debug.enable('*');
|
|
32
34
|
else if (ds.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
|
|
33
35
|
else debug.enable(`${env}:err,${env}:warn`);
|
|
34
36
|
this.ds = ds;
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
*
|
|
40
|
+
* @param {...any} args
|
|
41
|
+
*/ debug(...args) {
|
|
40
42
|
const first = args?.at(0);
|
|
41
43
|
const last = args?.at(-1);
|
|
42
|
-
if (typeof first ===
|
|
44
|
+
if (typeof first === 'string') {
|
|
43
45
|
args[0] = `${first}`;
|
|
44
46
|
this.ds.debug(...args);
|
|
45
|
-
} else if (typeof first ===
|
|
47
|
+
} else if (typeof first === 'object' && typeof last === 'string') this.ds.debug(`${last}:%O`, first);
|
|
46
48
|
// args[0] = `${this.pre}:${args[0]}`
|
|
47
49
|
// console.debug(...args)
|
|
48
50
|
// console.debug(this.pre, ...args)
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
*
|
|
54
|
+
* @param {...any} args
|
|
55
|
+
*/ error(...args) {
|
|
54
56
|
const first = args?.at(0);
|
|
55
57
|
const last = args?.at(-1);
|
|
56
|
-
if (typeof first ===
|
|
58
|
+
if (typeof first === 'string') {
|
|
57
59
|
args[0] = ` ${first}`;
|
|
58
60
|
this.ds.err(...args);
|
|
59
|
-
} else if (typeof first ===
|
|
61
|
+
} else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
|
|
60
62
|
}
|
|
61
63
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
*
|
|
65
|
+
* @param {...any} args
|
|
66
|
+
*/ err(...args) {
|
|
65
67
|
const first = args?.[0];
|
|
66
68
|
if (first?.message || first?.msg) {
|
|
67
69
|
args[0] = {
|
|
@@ -72,22 +74,22 @@ import debug from "debug";
|
|
|
72
74
|
this.error(...args);
|
|
73
75
|
}
|
|
74
76
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
*
|
|
78
|
+
* @param {...any} args
|
|
79
|
+
*/ warn(...args) {
|
|
78
80
|
const first = args?.at(0);
|
|
79
81
|
const last = args?.at(-1);
|
|
80
|
-
if (typeof first ===
|
|
81
|
-
else if (typeof first ===
|
|
82
|
+
if (typeof first === 'string') this.ds.warn(...args);
|
|
83
|
+
else if (typeof first === 'object' && typeof last === 'string') this.ds.warn(`${last}:%O`, first);
|
|
82
84
|
}
|
|
83
85
|
/**
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
*
|
|
87
|
+
* @param {...any} args
|
|
88
|
+
*/ info(...args) {
|
|
87
89
|
const first = args?.at(0);
|
|
88
90
|
const last = args?.at(-1);
|
|
89
|
-
if (typeof first ===
|
|
90
|
-
else if (typeof first ===
|
|
91
|
+
if (typeof first === 'string') this.ds.info(...args);
|
|
92
|
+
else if (typeof first === 'object' && typeof last === 'string') this.ds.info(`${last}:%O`, first);
|
|
91
93
|
}
|
|
92
94
|
}
|
|
93
95
|
/**
|
|
@@ -115,10 +117,14 @@ import debug from "debug";
|
|
|
115
117
|
}
|
|
116
118
|
/**
|
|
117
119
|
* 获取模块文件名称
|
|
120
|
+
* esm: import.meta.url or cjs: __filename
|
|
118
121
|
* @param {string} file
|
|
119
122
|
* @returns
|
|
120
123
|
*/ function name(file) {
|
|
124
|
+
// import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
|
|
125
|
+
file = fileURLToPath(file) // fileUrl 转成路径
|
|
126
|
+
;
|
|
121
127
|
const baseName = path.basename(file);
|
|
122
|
-
return baseName.replace(path.extname(baseName),
|
|
128
|
+
return baseName.replace(path.extname(baseName), '');
|
|
123
129
|
}
|
|
124
130
|
export { name, log };
|