From d92b3f979c105dd24a6ab578d9aebb073bde9c46 Mon Sep 17 00:00:00 2001 From: "Daniel Boelzle [:dbo]" Date: Thu, 25 Jul 2019 17:06:57 +0200 Subject: [PATCH] #719 - Support decoration with process information --- README.md | 2 ++ src/node.js | 7 ++++++- test.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88dae35d..a378329f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/node.js b/src/node.js index 5e1f1541..dbc4803a 100644 --- a/src/node.js +++ b/src/node.js @@ -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; diff --git a/test.js b/test.js index f61e079b..f555d4ad 100644 --- a/test.js +++ b/test.js @@ -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);