Skip to content

Commit 6d9b1e4

Browse files
TylerYangMylesBorins
authored andcommitted
util: allow wildcards in NODE_DEBUG variable
PR-URL: #17609 Fixes: #17605 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent ec443c3 commit 6d9b1e4

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

doc/api/util.md

+13
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ FOO 3245: hello from foo [123]
104104
where `3245` is the process id. If it is not run with that
105105
environment variable set, then it will not print anything.
106106

107+
The `section` supports wildcard also, for example:
108+
```js
109+
const util = require('util');
110+
const debuglog = util.debuglog('foo-bar');
111+
112+
debuglog('hi there, it\'s foo-bar [%d]', 2333);
113+
```
114+
115+
if it is run with `NODE_DEBUG=foo*` in the environment, then it will output something like:
116+
```txt
117+
FOO-BAR 3257: hi there, it's foo-bar [2333]
118+
```
119+
107120
Multiple comma-separated `section` names may be specified in the `NODE_DEBUG`
108121
environment variable. For example: `NODE_DEBUG=fs,net,tls`.
109122

lib/util.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,21 @@ function format(f) {
230230
return str;
231231
}
232232

233-
var debugs = {};
234-
var debugEnviron;
233+
const debugs = {};
234+
let debugEnvRegex = /^$/;
235+
if (process.env.NODE_DEBUG) {
236+
let debugEnv = process.env.NODE_DEBUG;
237+
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
238+
.replace(/\*/g, '.*')
239+
.replace(/,/g, '$|^')
240+
.toUpperCase();
241+
debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i');
242+
}
235243

236244
function debuglog(set) {
237-
if (debugEnviron === undefined) {
238-
debugEnviron = new Set(
239-
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
240-
}
241245
set = set.toUpperCase();
242246
if (!debugs[set]) {
243-
if (debugEnviron.has(set)) {
247+
if (debugEnvRegex.test(set)) {
244248
var pid = process.pid;
245249
debugs[set] = function() {
246250
var msg = exports.format.apply(exports, arguments);

test/sequential/test-util-debug.js

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function parent() {
4343
test('f$oo', true, 'f$oo');
4444
test('f$oo', false, 'f.oo');
4545
test('no-bar-at-all', false, 'bar');
46+
47+
test('test-abc', true, 'test-abc');
48+
test('test-a', false, 'test-abc');
49+
test('test-*', true, 'test-abc');
50+
test('test-*c', true, 'test-abc');
51+
test('test-*abc', true, 'test-abc');
52+
test('abc-test', true, 'abc-test');
53+
test('a*-test', true, 'abc-test');
54+
test('*-test', true, 'abc-test');
4655
}
4756

4857
function test(environ, shouldWrite, section) {

0 commit comments

Comments
 (0)