@wiajs/log 4.3.9 → 4.3.11

Sign up to get free protection for your applications and to get access to all the features.
package/dist/log.cjs ADDED
@@ -0,0 +1,160 @@
1
+ /*!
2
+ * wia log v4.3.11
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
+ const path = require('node:path');
11
+ const node_url = require('node:url');
12
+ const debug = require('debug');
13
+
14
+ /**
15
+ * debug日志封装,
16
+ * 使用方法:
17
+ * import {log as Log, filename} from './log.js'
18
+ * const log = Log({env: `wia:${filename(__filename)}`})
19
+ * log('hello')
20
+ * log({a: 1, b: 2}, 'fun')
21
+ * log.error/err/info/warn
22
+ * $env:DEBUG=* 全开
23
+ * $env:DEBUG=*:log 全开
24
+ * $env:DEBUG=*:info log 不开,其他全开
25
+ * warn 和 err 一直打开!
26
+ */
27
+ class Log {
28
+ /**
29
+ * 构造函数
30
+ * @param {*} opts {
31
+ * pre: 前缀,一般是模块名称,
32
+ * env: NODE_DEBUG 环境变量
33
+ * }
34
+ */
35
+ constructor(opts) {
36
+ let {env} = opts;
37
+ env = env ?? '';
38
+ const 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`);
43
+
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
+
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') this.ds.debug(`${last}:%O`, first);
63
+ // args[0] = `${this.pre}:${args[0]}`
64
+ // console.debug(...args)
65
+ // console.debug(this.pre, ...args)
66
+ }
67
+
68
+ /**
69
+ *
70
+ * @param {...any} args
71
+ */
72
+ error(...args) {
73
+ const first = args?.at(0);
74
+ const last = args?.at(-1);
75
+ if (typeof first === 'string') {
76
+ args[0] = ` ${first}`;
77
+ this.ds.err(...args);
78
+ } else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
79
+ }
80
+
81
+ /**
82
+ *
83
+ * @param {...any} args
84
+ */
85
+ err(...args) {
86
+ const first = args?.[0];
87
+ if (first?.message || first?.msg) {
88
+ args[0] = {exp: first.message || first.msg};
89
+ if (first?.code) args[0].exp += ` code:${first.code}`;
90
+ }
91
+ this.error(...args);
92
+ }
93
+
94
+ /**
95
+ *
96
+ * @param {...any} args
97
+ */
98
+ warn(...args) {
99
+ const first = args?.at(0);
100
+ const last = args?.at(-1);
101
+ if (typeof first === 'string') this.ds.warn(...args);
102
+ else if (typeof first === 'object' && typeof last === 'string') this.ds.warn(`${last}:%O`, first);
103
+ }
104
+
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
+ }
115
+ }
116
+
117
+ /**
118
+ * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
119
+ * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
120
+ * @param {...any} args - params
121
+ * returns {pino & (...args) => void}
122
+ */
123
+ function log(...args) {
124
+ const last = args.at(-1);
125
+
126
+ // 全局日志
127
+ if (args.length !== 1 || !last?.env) return
128
+
129
+ const {env} = last;
130
+ // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
131
+ const lg = new Log({env});
132
+
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);
141
+
142
+ return R
143
+ }
144
+
145
+ /**
146
+ * 获取模块文件名称
147
+ * esm: import.meta.url or cjs: __filename
148
+ * @param {string} file
149
+ * @returns
150
+ */
151
+ function name(file) {
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), '')
156
+ }
157
+
158
+ exports.default = Log;
159
+ exports.log = log;
160
+ exports.name = name;
package/dist/log.js ADDED
@@ -0,0 +1,177 @@
1
+ /*!
2
+ * wia log v4.3.11
3
+ * (c) 2024-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
+ }));
@@ -0,0 +1,6 @@
1
+ /*!
2
+ * wia log v4.3.11
3
+ * (c) 2024-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();
package/dist/log.mjs ADDED
@@ -0,0 +1,154 @@
1
+ /*!
2
+ * wia log v4.3.11
3
+ * (c) 2024-2024 Sibyl Yu and contributors
4
+ * Released under the MIT License.
5
+ */
6
+ import path from 'node:path';
7
+ import { fileURLToPath } from 'node:url';
8
+ import debug from 'debug';
9
+
10
+ /**
11
+ * debug日志封装,
12
+ * 使用方法:
13
+ * import {log as Log, filename} from './log.js'
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 一直打开!
22
+ */
23
+ class Log {
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`);
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;
46
+ }
47
+
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
+ }
63
+
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
+ }
76
+
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
+ }
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);
99
+ }
100
+
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
+ }
111
+ }
112
+
113
+ /**
114
+ * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
115
+ * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
116
+ * @param {...any} args - params
117
+ * returns {pino & (...args) => void}
118
+ */
119
+ function log(...args) {
120
+ const last = args.at(-1);
121
+
122
+ // 全局日志
123
+ if (args.length !== 1 || !last?.env) return
124
+
125
+ const {env} = last;
126
+ // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
127
+ const lg = new Log({env});
128
+
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);
137
+
138
+ return R
139
+ }
140
+
141
+ /**
142
+ * 获取模块文件名称
143
+ * esm: import.meta.url or cjs: __filename
144
+ * @param {string} file
145
+ * @returns
146
+ */
147
+ function name(file) {
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), '')
152
+ }
153
+
154
+ export { Log as default, log, name };
package/index.js CHANGED
@@ -1 +1 @@
1
- export { default, name, log } from "./lib/node";
1
+ export {default, name, log} from './lib/node.js'
package/lib/browser.js ADDED
@@ -0,0 +1,163 @@
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; // export { log, Log };
package/lib/node.js ADDED
@@ -0,0 +1,130 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import debug from 'debug';
4
+ /**
5
+ * debug日志封装,
6
+ * 使用方法:
7
+ * import {log as Log, filename} from './log.js'
8
+ * const log = Log({env: `wia:${filename(__filename)}`})
9
+ * log('hello')
10
+ * log({a: 1, b: 2}, 'fun')
11
+ * log.error/err/info/warn
12
+ * $env:DEBUG=* 全开
13
+ * $env:DEBUG=*:log 全开
14
+ * $env:DEBUG=*:info log 不开,其他全开
15
+ * warn 和 err 一直打开!
16
+ */ export default class Log {
17
+ /**
18
+ * 构造函数
19
+ * @param {*} opts {
20
+ * pre: 前缀,一般是模块名称,
21
+ * env: NODE_DEBUG 环境变量
22
+ * }
23
+ */ constructor(opts){
24
+ let { env } = opts;
25
+ env = env ?? '';
26
+ const ds = {} // debugs
27
+ ;
28
+ ds.debug = debug(`${env}`);
29
+ ds.info = debug(`${env}:info`);
30
+ ds.err = debug(`${env}:err`);
31
+ ds.warn = debug(`${env}:warn`);
32
+ // 仅最后调用生效,覆盖环境变量
33
+ if (ds.debug.enabled) debug.enable('*');
34
+ else if (ds.info.enabled) debug.enable(`${env}:info,${env}:err,${env}:warn`);
35
+ else debug.enable(`${env}:err,${env}:warn`);
36
+ this.ds = ds;
37
+ }
38
+ /**
39
+ *
40
+ * @param {...any} args
41
+ */ debug(...args) {
42
+ const first = args?.at(0);
43
+ const last = args?.at(-1);
44
+ if (typeof first === 'string') {
45
+ args[0] = `${first}`;
46
+ this.ds.debug(...args);
47
+ } else if (typeof first === 'object' && typeof last === 'string') this.ds.debug(`${last}:%O`, first);
48
+ // args[0] = `${this.pre}:${args[0]}`
49
+ // console.debug(...args)
50
+ // console.debug(this.pre, ...args)
51
+ }
52
+ /**
53
+ *
54
+ * @param {...any} args
55
+ */ error(...args) {
56
+ const first = args?.at(0);
57
+ const last = args?.at(-1);
58
+ if (typeof first === 'string') {
59
+ args[0] = ` ${first}`;
60
+ this.ds.err(...args);
61
+ } else if (typeof first === 'object' && typeof last === 'string') this.ds.err(` ${last}:%O`, first);
62
+ }
63
+ /**
64
+ *
65
+ * @param {...any} args
66
+ */ err(...args) {
67
+ const first = args?.[0];
68
+ if (first?.message || first?.msg) {
69
+ args[0] = {
70
+ exp: first.message || first.msg
71
+ };
72
+ if (first?.code) args[0].exp += ` code:${first.code}`;
73
+ }
74
+ this.error(...args);
75
+ }
76
+ /**
77
+ *
78
+ * @param {...any} args
79
+ */ warn(...args) {
80
+ const first = args?.at(0);
81
+ const last = args?.at(-1);
82
+ if (typeof first === 'string') this.ds.warn(...args);
83
+ else if (typeof first === 'object' && typeof last === 'string') this.ds.warn(`${last}:%O`, first);
84
+ }
85
+ /**
86
+ *
87
+ * @param {...any} args
88
+ */ info(...args) {
89
+ const first = args?.at(0);
90
+ const last = args?.at(-1);
91
+ if (typeof first === 'string') this.ds.info(...args);
92
+ else if (typeof first === 'object' && typeof last === 'string') this.ds.info(`${last}:%O`, first);
93
+ }
94
+ }
95
+ /**
96
+ * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
97
+ * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
98
+ * @param {...any} args - params
99
+ * returns {pino & (...args) => void}
100
+ */ function log(...args) {
101
+ const last = args.at(-1);
102
+ // 全局日志
103
+ if (args.length !== 1 || !last?.env) return;
104
+ const { env } = last;
105
+ // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
106
+ const lg = new Log({
107
+ env
108
+ });
109
+ /** @param {*} args2 */ const R = (...args2)=>lg.debug(...args2);
110
+ R.debug = lg.debug.bind(lg);
111
+ R.info = lg.info.bind(lg);
112
+ R.warn = lg.warn.bind(lg);
113
+ R.info = lg.info.bind(lg);
114
+ R.error = lg.error.bind(lg);
115
+ R.err = lg.err.bind(lg);
116
+ return R;
117
+ }
118
+ /**
119
+ * 获取模块文件名称
120
+ * esm: import.meta.url or cjs: __filename
121
+ * @param {string} file
122
+ * @returns
123
+ */ function name(file) {
124
+ // import.meta.url: 'file:///D:/prj/wiajs/req/test/log.t.js'
125
+ file = fileURLToPath(file) // fileUrl 转成路径
126
+ ;
127
+ const baseName = path.basename(file);
128
+ return baseName.replace(path.extname(baseName), '');
129
+ }
130
+ 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.9",
4
+ "version": "4.3.11",
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 };
package/src/node.js DELETED
@@ -1,150 +0,0 @@
1
- import path from "node:path";
2
- import debug from "debug";
3
-
4
- /**
5
- * debug日志封装,
6
- * 使用方法:
7
- * import {log as Log, filename} from './log.js'
8
- * const log = Log({env: `wia:${filename(__filename)}`})
9
- * log('hello')
10
- * log({a: 1, b: 2}, 'fun')
11
- * log.error/err/info/warn
12
- * $env:DEBUG=* 全开
13
- * $env:DEBUG=*:log 全开
14
- * $env:DEBUG=*:info log 不开,其他全开
15
- * warn 和 err 一直打开!
16
- */
17
- export default class Log {
18
- /**
19
- * 构造函数
20
- * @param {*} opts {
21
- * pre: 前缀,一般是模块名称,
22
- * env: NODE_DEBUG 环境变量
23
- * }
24
- */
25
- constructor(opts) {
26
- let { env } = opts;
27
- env = env ?? "";
28
- const ds = {}; // debugs
29
- ds.debug = debug(`${env}`);
30
- ds.info = debug(`${env}:info`);
31
- ds.err = debug(`${env}:err`);
32
- ds.warn = debug(`${env}:warn`);
33
-
34
- // 仅最后调用生效,覆盖环境变量
35
- if (ds.debug.enabled) debug.enable("*");
36
- else if (ds.info.enabled)
37
- debug.enable(`${env}:info,${env}:err,${env}:warn`);
38
- else debug.enable(`${env}:err,${env}:warn`);
39
-
40
- this.ds = ds;
41
- }
42
-
43
- /**
44
- *
45
- * @param {...any} args
46
- */
47
- debug(...args) {
48
- const first = args?.at(0);
49
- const last = args?.at(-1);
50
- if (typeof first === "string") {
51
- args[0] = `${first}`;
52
- this.ds.debug(...args);
53
- } else if (typeof first === "object" && typeof last === "string")
54
- this.ds.debug(`${last}:%O`, first);
55
- // args[0] = `${this.pre}:${args[0]}`
56
- // console.debug(...args)
57
- // console.debug(this.pre, ...args)
58
- }
59
-
60
- /**
61
- *
62
- * @param {...any} args
63
- */
64
- error(...args) {
65
- const first = args?.at(0);
66
- const last = args?.at(-1);
67
- if (typeof first === "string") {
68
- args[0] = ` ${first}`;
69
- this.ds.err(...args);
70
- } else if (typeof first === "object" && typeof last === "string")
71
- this.ds.err(` ${last}:%O`, first);
72
- }
73
-
74
- /**
75
- *
76
- * @param {...any} args
77
- */
78
- err(...args) {
79
- const first = args?.[0];
80
- if (first?.message || first?.msg) {
81
- args[0] = { exp: first.message || first.msg };
82
- if (first?.code) args[0].exp += ` code:${first.code}`;
83
- }
84
- this.error(...args);
85
- }
86
-
87
- /**
88
- *
89
- * @param {...any} args
90
- */
91
- warn(...args) {
92
- const first = args?.at(0);
93
- const last = args?.at(-1);
94
- if (typeof first === "string") this.ds.warn(...args);
95
- else if (typeof first === "object" && typeof last === "string")
96
- this.ds.warn(`${last}:%O`, first);
97
- }
98
-
99
- /**
100
- *
101
- * @param {...any} args
102
- */
103
- info(...args) {
104
- const first = args?.at(0);
105
- const last = args?.at(-1);
106
- if (typeof first === "string") this.ds.info(...args);
107
- else if (typeof first === "object" && typeof last === "string")
108
- this.ds.info(`${last}:%O`, first);
109
- }
110
- }
111
-
112
- /**
113
- * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
114
- * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
115
- * @param {...any} args - params
116
- * returns {pino & (...args) => void}
117
- */
118
- function log(...args) {
119
- const last = args.at(-1);
120
-
121
- // 全局日志
122
- if (args.length !== 1 || !last?.env) return;
123
-
124
- const { env } = last;
125
- // 唯一 env 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
126
- const lg = new Log({ env });
127
-
128
- /** @param {*} args2 */
129
- const R = (...args2) => lg.debug(...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.error = lg.error.bind(lg);
135
- R.err = lg.err.bind(lg);
136
-
137
- return R;
138
- }
139
-
140
- /**
141
- * 获取模块文件名称
142
- * @param {string} file
143
- * @returns
144
- */
145
- function name(file) {
146
- const baseName = path.basename(file);
147
- return baseName.replace(path.extname(baseName), "");
148
- }
149
-
150
- export { name, log };