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