Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ is wrapped in an `Error` with the original value stored in a field named
added: v0.11.3
-->

* `section` {string} A string identifying the portion of the application for
* `section` {string} A wildcard string identifying the portion of the application for
which the `debuglog` function is being created.
* Returns: {Function} The logging function

Expand All @@ -89,16 +89,16 @@ For example:

```js
const util = require('util');
const debuglog = util.debuglog('foo');
const debuglog = util.debuglog('foo-bar');

debuglog('hello from foo [%d]', 123);
```

If this program is run with `NODE_DEBUG=foo` in the environment, then
If this program is run with `NODE_DEBUG=foo*` or `NODE_DEBUG=foo-bar` in the environment, then
it will output something like:

```txt
FOO 3245: hello from foo [123]
FOO-BAR 3245: hello from foo [123]
```

where `3245` is the process id. If it is not run with that
Expand Down
20 changes: 11 additions & 9 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,30 @@ function format(f) {
return str;
}

var debugs = {};
var debugEnviron;
const debugs = {};
let debugEnviron;

const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g;

function stringToRegExp(string) {
function arrayToRegExp(strArr) {
// Escape special chars except wildcard.
string = string.replace(regExpSpecialChars, '\\$&');
// Transform wildcard to "match anything"
string = string.replace(/\*/g, '.*');
return new RegExp(`^${string}$`);
strArr = strArr.map((str) => {
return str.replace(regExpSpecialChars, '\\$&')
.replace(/\*/g, '.*');
});

return new RegExp(strArr.join('|'), 'i');
}

function debuglog(set) {
if (debugEnviron === undefined) {
debugEnviron = Array.from(new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
debugEnviron = debugEnviron.map(stringToRegExp);
}
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnviron.some((name) => name.test(set))) {
const regex = arrayToRegExp(debugEnviron);
if (regex.test(set)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down