@wiajs/log 4.3.8
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +481 -0
- package/index.js +1 -0
- package/package.json +79 -0
- package/src/browser.js +213 -0
- package/src/node.js +150 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2014-2017 TJ Holowaychuk <tj@vision-media.ca>
|
4
|
+
Copyright (c) 2018-2021 Josh Junon
|
5
|
+
Copyright (c) 2022-2024 Sibyl Yu <sibyl@wia.pub>
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
8
|
+
and associated documentation files (the 'Software'), to deal in the Software without restriction,
|
9
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
10
|
+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
11
|
+
subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
14
|
+
portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
17
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
19
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
package/README.md
ADDED
@@ -0,0 +1,481 @@
|
|
1
|
+
# debug
|
2
|
+
[![OpenCollective](https://opencollective.com/debug/backers/badge.svg)](#backers)
|
3
|
+
[![OpenCollective](https://opencollective.com/debug/sponsors/badge.svg)](#sponsors)
|
4
|
+
|
5
|
+
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
|
6
|
+
|
7
|
+
A tiny JavaScript debugging utility modelled after Node.js core's debugging
|
8
|
+
technique. Works in Node.js and web browsers.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
```bash
|
13
|
+
$ npm install debug
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
`debug` exposes a function; simply pass this function the name of your module, and it will return a decorated version of `console.error` for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.
|
19
|
+
|
20
|
+
Example [_app.js_](./examples/node/app.js):
|
21
|
+
|
22
|
+
```js
|
23
|
+
var debug = require('debug')('http')
|
24
|
+
, http = require('http')
|
25
|
+
, name = 'My App';
|
26
|
+
|
27
|
+
// fake app
|
28
|
+
|
29
|
+
debug('booting %o', name);
|
30
|
+
|
31
|
+
http.createServer(function(req, res){
|
32
|
+
debug(req.method + ' ' + req.url);
|
33
|
+
res.end('hello\n');
|
34
|
+
}).listen(3000, function(){
|
35
|
+
debug('listening');
|
36
|
+
});
|
37
|
+
|
38
|
+
// fake worker of some kind
|
39
|
+
|
40
|
+
require('./worker');
|
41
|
+
```
|
42
|
+
|
43
|
+
Example [_worker.js_](./examples/node/worker.js):
|
44
|
+
|
45
|
+
```js
|
46
|
+
var a = require('debug')('worker:a')
|
47
|
+
, b = require('debug')('worker:b');
|
48
|
+
|
49
|
+
function work() {
|
50
|
+
a('doing lots of uninteresting work');
|
51
|
+
setTimeout(work, Math.random() * 1000);
|
52
|
+
}
|
53
|
+
|
54
|
+
work();
|
55
|
+
|
56
|
+
function workb() {
|
57
|
+
b('doing some work');
|
58
|
+
setTimeout(workb, Math.random() * 2000);
|
59
|
+
}
|
60
|
+
|
61
|
+
workb();
|
62
|
+
```
|
63
|
+
|
64
|
+
The `DEBUG` environment variable is then used to enable these based on space or
|
65
|
+
comma-delimited names.
|
66
|
+
|
67
|
+
Here are some examples:
|
68
|
+
|
69
|
+
<img width="647" alt="screen shot 2017-08-08 at 12 53 04 pm" src="https://user-images.githubusercontent.com/71256/29091703-a6302cdc-7c38-11e7-8304-7c0b3bc600cd.png">
|
70
|
+
<img width="647" alt="screen shot 2017-08-08 at 12 53 38 pm" src="https://user-images.githubusercontent.com/71256/29091700-a62a6888-7c38-11e7-800b-db911291ca2b.png">
|
71
|
+
<img width="647" alt="screen shot 2017-08-08 at 12 53 25 pm" src="https://user-images.githubusercontent.com/71256/29091701-a62ea114-7c38-11e7-826a-2692bedca740.png">
|
72
|
+
|
73
|
+
#### Windows command prompt notes
|
74
|
+
|
75
|
+
##### CMD
|
76
|
+
|
77
|
+
On Windows the environment variable is set using the `set` command.
|
78
|
+
|
79
|
+
```cmd
|
80
|
+
set DEBUG=*,-not_this
|
81
|
+
```
|
82
|
+
|
83
|
+
Example:
|
84
|
+
|
85
|
+
```cmd
|
86
|
+
set DEBUG=* & node app.js
|
87
|
+
```
|
88
|
+
|
89
|
+
##### PowerShell (VS Code default)
|
90
|
+
|
91
|
+
PowerShell uses different syntax to set environment variables.
|
92
|
+
|
93
|
+
```cmd
|
94
|
+
$env:DEBUG = "*,-not_this"
|
95
|
+
```
|
96
|
+
|
97
|
+
Example:
|
98
|
+
|
99
|
+
```cmd
|
100
|
+
$env:DEBUG='app';node app.js
|
101
|
+
```
|
102
|
+
|
103
|
+
Then, run the program to be debugged as usual.
|
104
|
+
|
105
|
+
npm script example:
|
106
|
+
```js
|
107
|
+
"windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",
|
108
|
+
```
|
109
|
+
|
110
|
+
## Namespace Colors
|
111
|
+
|
112
|
+
Every debug instance has a color generated for it based on its namespace name.
|
113
|
+
This helps when visually parsing the debug output to identify which debug instance
|
114
|
+
a debug line belongs to.
|
115
|
+
|
116
|
+
#### Node.js
|
117
|
+
|
118
|
+
In Node.js, colors are enabled when stderr is a TTY. You also _should_ install
|
119
|
+
the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,
|
120
|
+
otherwise debug will only use a small handful of basic colors.
|
121
|
+
|
122
|
+
<img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">
|
123
|
+
|
124
|
+
#### Web Browser
|
125
|
+
|
126
|
+
Colors are also enabled on "Web Inspectors" that understand the `%c` formatting
|
127
|
+
option. These are WebKit web inspectors, Firefox ([since version
|
128
|
+
31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))
|
129
|
+
and the Firebug plugin for Firefox (any version).
|
130
|
+
|
131
|
+
<img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">
|
132
|
+
|
133
|
+
|
134
|
+
## Millisecond diff
|
135
|
+
|
136
|
+
When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
|
137
|
+
|
138
|
+
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
|
139
|
+
|
140
|
+
When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:
|
141
|
+
|
142
|
+
<img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">
|
143
|
+
|
144
|
+
|
145
|
+
## Conventions
|
146
|
+
|
147
|
+
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable. You can then use it for normal output as well as debug output.
|
148
|
+
|
149
|
+
## Wildcards
|
150
|
+
|
151
|
+
The `*` character may be used as a wildcard. Suppose for example your library has
|
152
|
+
debuggers named "connect:bodyParser", "connect:compress", "connect:session",
|
153
|
+
instead of listing all three with
|
154
|
+
`DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do
|
155
|
+
`DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
|
156
|
+
|
157
|
+
You can also exclude specific debuggers by prefixing them with a "-" character.
|
158
|
+
For example, `DEBUG=*,-connect:*` would include all debuggers except those
|
159
|
+
starting with "connect:".
|
160
|
+
|
161
|
+
## Environment Variables
|
162
|
+
|
163
|
+
When running through Node.js, you can set a few environment variables that will
|
164
|
+
change the behavior of the debug logging:
|
165
|
+
|
166
|
+
| Name | Purpose |
|
167
|
+
|-----------|-------------------------------------------------|
|
168
|
+
| `DEBUG` | Enables/disables specific debugging namespaces. |
|
169
|
+
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
|
170
|
+
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
|
171
|
+
| `DEBUG_DEPTH` | Object inspection depth. |
|
172
|
+
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
|
173
|
+
|
174
|
+
|
175
|
+
__Note:__ The environment variables beginning with `DEBUG_` end up being
|
176
|
+
converted into an Options object that gets used with `%o`/`%O` formatters.
|
177
|
+
See the Node.js documentation for
|
178
|
+
[`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)
|
179
|
+
for the complete list.
|
180
|
+
|
181
|
+
## Formatters
|
182
|
+
|
183
|
+
Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
|
184
|
+
Below are the officially supported formatters:
|
185
|
+
|
186
|
+
| Formatter | Representation |
|
187
|
+
|-----------|----------------|
|
188
|
+
| `%O` | Pretty-print an Object on multiple lines. |
|
189
|
+
| `%o` | Pretty-print an Object all on a single line. |
|
190
|
+
| `%s` | String. |
|
191
|
+
| `%d` | Number (both integer and float). |
|
192
|
+
| `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
|
193
|
+
| `%%` | Single percent sign ('%'). This does not consume an argument. |
|
194
|
+
|
195
|
+
|
196
|
+
### Custom formatters
|
197
|
+
|
198
|
+
You can add custom formatters by extending the `debug.formatters` object.
|
199
|
+
For example, if you wanted to add support for rendering a Buffer as hex with
|
200
|
+
`%h`, you could do something like:
|
201
|
+
|
202
|
+
```js
|
203
|
+
const createDebug = require('debug')
|
204
|
+
createDebug.formatters.h = (v) => {
|
205
|
+
return v.toString('hex')
|
206
|
+
}
|
207
|
+
|
208
|
+
// …elsewhere
|
209
|
+
const debug = createDebug('foo')
|
210
|
+
debug('this is hex: %h', new Buffer('hello world'))
|
211
|
+
// foo this is hex: 68656c6c6f20776f726c6421 +0ms
|
212
|
+
```
|
213
|
+
|
214
|
+
|
215
|
+
## Browser Support
|
216
|
+
|
217
|
+
You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),
|
218
|
+
or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),
|
219
|
+
if you don't want to build it yourself.
|
220
|
+
|
221
|
+
Debug's enable state is currently persisted by `localStorage`.
|
222
|
+
Consider the situation shown below where you have `worker:a` and `worker:b`,
|
223
|
+
and wish to debug both. You can enable this using `localStorage.debug`:
|
224
|
+
|
225
|
+
```js
|
226
|
+
localStorage.debug = 'worker:*'
|
227
|
+
```
|
228
|
+
|
229
|
+
And then refresh the page.
|
230
|
+
|
231
|
+
```js
|
232
|
+
a = debug('worker:a');
|
233
|
+
b = debug('worker:b');
|
234
|
+
|
235
|
+
setInterval(function(){
|
236
|
+
a('doing some work');
|
237
|
+
}, 1000);
|
238
|
+
|
239
|
+
setInterval(function(){
|
240
|
+
b('doing some work');
|
241
|
+
}, 1200);
|
242
|
+
```
|
243
|
+
|
244
|
+
In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScript console will—by default—only show messages logged by `debug` if the "Verbose" log level is _enabled_.
|
245
|
+
|
246
|
+
<img width="647" src="https://user-images.githubusercontent.com/7143133/152083257-29034707-c42c-4959-8add-3cee850e6fcf.png">
|
247
|
+
|
248
|
+
## Output streams
|
249
|
+
|
250
|
+
By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:
|
251
|
+
|
252
|
+
Example [_stdout.js_](./examples/node/stdout.js):
|
253
|
+
|
254
|
+
```js
|
255
|
+
var debug = require('debug');
|
256
|
+
var error = debug('app:error');
|
257
|
+
|
258
|
+
// by default stderr is used
|
259
|
+
error('goes to stderr!');
|
260
|
+
|
261
|
+
var log = debug('app:log');
|
262
|
+
// set this namespace to log via console.log
|
263
|
+
log.log = console.log.bind(console); // don't forget to bind to console!
|
264
|
+
log('goes to stdout');
|
265
|
+
error('still goes to stderr!');
|
266
|
+
|
267
|
+
// set all output to go via console.info
|
268
|
+
// overrides all per-namespace log settings
|
269
|
+
debug.log = console.info.bind(console);
|
270
|
+
error('now goes to stdout via console.info');
|
271
|
+
log('still goes to stdout, but via console.info now');
|
272
|
+
```
|
273
|
+
|
274
|
+
## Extend
|
275
|
+
You can simply extend debugger
|
276
|
+
```js
|
277
|
+
const log = require('debug')('auth');
|
278
|
+
|
279
|
+
//creates new debug instance with extended namespace
|
280
|
+
const logSign = log.extend('sign');
|
281
|
+
const logLogin = log.extend('login');
|
282
|
+
|
283
|
+
log('hello'); // auth hello
|
284
|
+
logSign('hello'); //auth:sign hello
|
285
|
+
logLogin('hello'); //auth:login hello
|
286
|
+
```
|
287
|
+
|
288
|
+
## Set dynamically
|
289
|
+
|
290
|
+
You can also enable debug dynamically by calling the `enable()` method :
|
291
|
+
|
292
|
+
```js
|
293
|
+
let debug = require('debug');
|
294
|
+
|
295
|
+
console.log(1, debug.enabled('test'));
|
296
|
+
|
297
|
+
debug.enable('test');
|
298
|
+
console.log(2, debug.enabled('test'));
|
299
|
+
|
300
|
+
debug.disable();
|
301
|
+
console.log(3, debug.enabled('test'));
|
302
|
+
|
303
|
+
```
|
304
|
+
|
305
|
+
print :
|
306
|
+
```
|
307
|
+
1 false
|
308
|
+
2 true
|
309
|
+
3 false
|
310
|
+
```
|
311
|
+
|
312
|
+
Usage :
|
313
|
+
`enable(namespaces)`
|
314
|
+
`namespaces` can include modes separated by a colon and wildcards.
|
315
|
+
|
316
|
+
Note that calling `enable()` completely overrides previously set DEBUG variable :
|
317
|
+
|
318
|
+
```
|
319
|
+
$ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
|
320
|
+
=> false
|
321
|
+
```
|
322
|
+
|
323
|
+
`disable()`
|
324
|
+
|
325
|
+
Will disable all namespaces. The functions returns the namespaces currently
|
326
|
+
enabled (and skipped). This can be useful if you want to disable debugging
|
327
|
+
temporarily without knowing what was enabled to begin with.
|
328
|
+
|
329
|
+
For example:
|
330
|
+
|
331
|
+
```js
|
332
|
+
let debug = require('debug');
|
333
|
+
debug.enable('foo:*,-foo:bar');
|
334
|
+
let namespaces = debug.disable();
|
335
|
+
debug.enable(namespaces);
|
336
|
+
```
|
337
|
+
|
338
|
+
Note: There is no guarantee that the string will be identical to the initial
|
339
|
+
enable string, but semantically they will be identical.
|
340
|
+
|
341
|
+
## Checking whether a debug target is enabled
|
342
|
+
|
343
|
+
After you've created a debug instance, you can determine whether or not it is
|
344
|
+
enabled by checking the `enabled` property:
|
345
|
+
|
346
|
+
```javascript
|
347
|
+
const debug = require('debug')('http');
|
348
|
+
|
349
|
+
if (debug.enabled) {
|
350
|
+
// do stuff...
|
351
|
+
}
|
352
|
+
```
|
353
|
+
|
354
|
+
You can also manually toggle this property to force the debug instance to be
|
355
|
+
enabled or disabled.
|
356
|
+
|
357
|
+
## Usage in child processes
|
358
|
+
|
359
|
+
Due to the way `debug` detects if the output is a TTY or not, colors are not shown in child processes when `stderr` is piped. A solution is to pass the `DEBUG_COLORS=1` environment variable to the child process.
|
360
|
+
For example:
|
361
|
+
|
362
|
+
```javascript
|
363
|
+
worker = fork(WORKER_WRAP_PATH, [workerPath], {
|
364
|
+
stdio: [
|
365
|
+
/* stdin: */ 0,
|
366
|
+
/* stdout: */ 'pipe',
|
367
|
+
/* stderr: */ 'pipe',
|
368
|
+
'ipc',
|
369
|
+
],
|
370
|
+
env: Object.assign({}, process.env, {
|
371
|
+
DEBUG_COLORS: 1 // without this settings, colors won't be shown
|
372
|
+
}),
|
373
|
+
});
|
374
|
+
|
375
|
+
worker.stderr.pipe(process.stderr, { end: false });
|
376
|
+
```
|
377
|
+
|
378
|
+
|
379
|
+
## Authors
|
380
|
+
|
381
|
+
- TJ Holowaychuk
|
382
|
+
- Nathan Rajlich
|
383
|
+
- Andrew Rhyne
|
384
|
+
- Josh Junon
|
385
|
+
|
386
|
+
## Backers
|
387
|
+
|
388
|
+
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]
|
389
|
+
|
390
|
+
<a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>
|
391
|
+
<a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>
|
392
|
+
<a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>
|
393
|
+
<a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>
|
394
|
+
<a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>
|
395
|
+
<a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>
|
396
|
+
<a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>
|
397
|
+
<a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>
|
398
|
+
<a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>
|
399
|
+
<a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>
|
400
|
+
<a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>
|
401
|
+
<a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>
|
402
|
+
<a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>
|
403
|
+
<a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>
|
404
|
+
<a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>
|
405
|
+
<a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>
|
406
|
+
<a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>
|
407
|
+
<a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>
|
408
|
+
<a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>
|
409
|
+
<a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>
|
410
|
+
<a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>
|
411
|
+
<a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>
|
412
|
+
<a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>
|
413
|
+
<a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>
|
414
|
+
<a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>
|
415
|
+
<a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>
|
416
|
+
<a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>
|
417
|
+
<a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>
|
418
|
+
<a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>
|
419
|
+
<a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>
|
420
|
+
|
421
|
+
|
422
|
+
## Sponsors
|
423
|
+
|
424
|
+
Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/debug#sponsor)]
|
425
|
+
|
426
|
+
<a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>
|
427
|
+
<a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>
|
428
|
+
<a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>
|
429
|
+
<a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>
|
430
|
+
<a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>
|
431
|
+
<a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>
|
432
|
+
<a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>
|
433
|
+
<a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>
|
434
|
+
<a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>
|
435
|
+
<a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>
|
436
|
+
<a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>
|
437
|
+
<a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>
|
438
|
+
<a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>
|
439
|
+
<a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>
|
440
|
+
<a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>
|
441
|
+
<a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>
|
442
|
+
<a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>
|
443
|
+
<a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>
|
444
|
+
<a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>
|
445
|
+
<a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>
|
446
|
+
<a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>
|
447
|
+
<a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>
|
448
|
+
<a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>
|
449
|
+
<a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>
|
450
|
+
<a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>
|
451
|
+
<a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>
|
452
|
+
<a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>
|
453
|
+
<a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>
|
454
|
+
<a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>
|
455
|
+
<a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>
|
456
|
+
|
457
|
+
## License
|
458
|
+
|
459
|
+
(The MIT License)
|
460
|
+
|
461
|
+
Copyright (c) 2014-2017 TJ Holowaychuk <tj@vision-media.ca>
|
462
|
+
Copyright (c) 2018-2021 Josh Junon
|
463
|
+
|
464
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
465
|
+
a copy of this software and associated documentation files (the
|
466
|
+
'Software'), to deal in the Software without restriction, including
|
467
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
468
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
469
|
+
permit persons to whom the Software is furnished to do so, subject to
|
470
|
+
the following conditions:
|
471
|
+
|
472
|
+
The above copyright notice and this permission notice shall be
|
473
|
+
included in all copies or substantial portions of the Software.
|
474
|
+
|
475
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
476
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
477
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
478
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
479
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
480
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
481
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { default, name, log } from "./lib/node";
|
package/package.json
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
{
|
2
|
+
"name": "@wiajs/log",
|
3
|
+
"description": "Lightweight debugging utility for Node.js and the browser",
|
4
|
+
"version": "4.3.8",
|
5
|
+
"type": "module",
|
6
|
+
"main": "index.js",
|
7
|
+
"browser": "./lib/browser.js",
|
8
|
+
"exports": {
|
9
|
+
".": {
|
10
|
+
"types": {
|
11
|
+
"require": "./index.d.cts",
|
12
|
+
"default": "./index.d.ts"
|
13
|
+
},
|
14
|
+
"node": {
|
15
|
+
"require": "./dist/log.cjs",
|
16
|
+
"default": "./index.js"
|
17
|
+
},
|
18
|
+
"browser": {
|
19
|
+
"require": "./lib/browser.js",
|
20
|
+
"default": "./lib/browser.js"
|
21
|
+
},
|
22
|
+
"default": {
|
23
|
+
"require": "./dist/log.cjs",
|
24
|
+
"default": "./dist/log.mjs"
|
25
|
+
}
|
26
|
+
},
|
27
|
+
"./package.json": "./package.json"
|
28
|
+
},
|
29
|
+
"scripts": {
|
30
|
+
"build": "cross-env NODE_ENV=production gulp build -f gulpfile.js",
|
31
|
+
"lib": "swc --config-file ./.swcrc ./src -d lib -w --strip-leading-paths",
|
32
|
+
"lint": "xo",
|
33
|
+
"test": "npm run test:node && npm run test:browser && npm run lint",
|
34
|
+
"test:node": "istanbul cover _mocha -- test.js test.node.js",
|
35
|
+
"test:browser": "karma start --single-run",
|
36
|
+
"test:coverage": "cat ./coverage/lcov.info | coveralls"
|
37
|
+
},
|
38
|
+
"keywords": ["debug", "log", "debugger"],
|
39
|
+
"files": ["src", "LICENSE", "README.md"],
|
40
|
+
"repository": {
|
41
|
+
"type": "git",
|
42
|
+
"url": "https://github.com/wiajs/log"
|
43
|
+
},
|
44
|
+
"author": {
|
45
|
+
"name": "Sibyl Yu",
|
46
|
+
"email": "sibyl@wia.pub"
|
47
|
+
},
|
48
|
+
"contributors": [
|
49
|
+
"Josh Junon (https://github.com/qix-)",
|
50
|
+
"TJ Holowaychuk <tj@vision-media.ca>",
|
51
|
+
"Nathan Rajlich <nathan@tootallnate.net> (http://n8.io)",
|
52
|
+
"Andrew Rhyne <rhyneandrew@gmail.com>"
|
53
|
+
],
|
54
|
+
"license": "MIT",
|
55
|
+
"publishConfig": {
|
56
|
+
"access": "public"
|
57
|
+
},
|
58
|
+
"dependencies": {
|
59
|
+
"debug": "^4.3.7"
|
60
|
+
},
|
61
|
+
"devDependencies": {
|
62
|
+
"brfs": "^2.0.1",
|
63
|
+
"browserify": "^16.2.3",
|
64
|
+
"coveralls": "^3.0.2",
|
65
|
+
"istanbul": "^0.4.5",
|
66
|
+
"karma": "^3.1.4",
|
67
|
+
"karma-browserify": "^6.0.0",
|
68
|
+
"karma-chrome-launcher": "^2.2.0",
|
69
|
+
"karma-mocha": "^1.3.0",
|
70
|
+
"mocha": "^5.2.0",
|
71
|
+
"mocha-lcov-reporter": "^1.2.0",
|
72
|
+
"sinon": "^14.0.0",
|
73
|
+
"xo": "^0.23.0"
|
74
|
+
},
|
75
|
+
"peerDependenciesMeta": {},
|
76
|
+
"engines": {
|
77
|
+
"node": ">=6.0"
|
78
|
+
}
|
79
|
+
}
|
package/src/browser.js
ADDED
@@ -0,0 +1,213 @@
|
|
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
ADDED
@@ -0,0 +1,150 @@
|
|
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 };
|