Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#719 - Support decoration with process information #720

Closed
wants to merge 3 commits into from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ change the behavior of the debug logging:
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
| `DEBUG_PROCESS` | Prefixes logs with a title in square brackets. |
| `DEBUG_PROCESS_PID` | Prefixes logs with the process id in square brackets. |


__Note:__ The environment variables beginning with `DEBUG_` end up being
Expand Down
27 changes: 17 additions & 10 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,18 @@ exports.inspectOpts = Object.keys(process.env).filter(key => {
return k.toUpperCase();
});

// Coerce string value into JS value
let val = process.env[key];
if (/^(yes|on|true|enabled)$/i.test(val)) {
val = true;
} else if (/^(no|off|false|disabled)$/i.test(val)) {
val = false;
} else if (val === 'null') {
val = null;
} else {
val = Number(val);
if (prop !== 'process') { // Don't coerce known string values
// Coerce string value into JS value
if (/^(yes|on|true|enabled)$/i.test(val)) {
val = true;
} else if (/^(no|off|false|disabled)$/i.test(val)) {
val = false;
} else if (val === 'null') {
val = null;
} else {
val = Number(val);
}
}

obj[prop] = val;
Expand All @@ -161,7 +163,12 @@ function useColors() {
*/

function formatArgs(args) {
const {namespace: name, useColors} = this;
let {namespace: name, useColors} = this;

const proc = exports.inspectOpts.process || (exports.inspectOpts.processPid ? process.pid : undefined);
if (proc) {
name = `[${proc}] ${name}`;
}

if (useColors) {
const c = this.color;
Expand Down
34 changes: 34 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ describe('debug', () => {
assert.doesNotThrow(() => debug.enable(true));
});

it('honors process decoration', function () {
const options = debug.inspectOpts;
if (!options) { // Browser
this.skip();
return;
}
const optionKeys = ['process', 'processPid', 'colors', 'hideDate'];
const oldValues = optionKeys.map(k => debug.inspectOpts[k]);

try {
options.colors = false;
options.hideDate = true;

let messageArgs;
const log = debug('bar');
log.enabled = true;
log.log = (...args) => {
messageArgs = args;
};

options.processPid = true;
log('baz');
assert.strictEqual(messageArgs[0], `[${process.pid}] bar baz`, 'should reflect DEBUG_PROCESS_PID');

options.process = 'foo';
log('baz');
assert.strictEqual(messageArgs[0], '[foo] bar baz', 'should reflect DEBUG_PROCESS');
} finally {
optionKeys.forEach((k, i) => {
options[k] = oldValues[i];
});
}
});

it('honors global debug namespace enable calls', () => {
assert.deepStrictEqual(debug('test:12345').enabled, false);
assert.deepStrictEqual(debug('test:67890').enabled, false);
Expand Down