Skip to content

Commit

Permalink
debug-js#719 - Support decoration with process information
Browse files Browse the repository at this point in the history
  • Loading branch information
dbo committed Jul 25, 2019
1 parent 5c7c61d commit d92b3f9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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
7 changes: 6 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,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
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ 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.equal(messageArgs[0], `[${process.pid}] bar baz`, 'should reflect DEBUG_PROCESS_PID');

options.process = 'foo';
log('baz');
assert.equal(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

0 comments on commit d92b3f9

Please sign in to comment.