From 9481d4693a25aadb8129a5d5e882c811dcd050da Mon Sep 17 00:00:00 2001 From: Gerard Hickey Date: Wed, 5 Jun 2024 17:26:42 -0400 Subject: [PATCH] Add option to log to STDOUT Signed-off-by: Gerard Hickey --- README.md | 15 ++++++++------- src/node.js | 6 +++++- test.node.js | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e9c3e047..dc76f84f 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,7 @@ 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_USE_STDOUT` | Use STDOUT instead of STDERR for messages | __Note:__ The environment variables beginning with `DEBUG_` end up being @@ -272,7 +273,7 @@ log('still goes to stdout, but via console.info now'); ``` ## Extend -You can simply extend debugger +You can simply extend debugger ```js const log = require('debug')('auth'); @@ -302,18 +303,18 @@ console.log(3, debug.enabled('test')); ``` -print : +print : ``` 1 false 2 true 3 false ``` -Usage : -`enable(namespaces)` +Usage : +`enable(namespaces)` `namespaces` can include modes separated by a colon and wildcards. - -Note that calling `enable()` completely overrides previously set DEBUG variable : + +Note that calling `enable()` completely overrides previously set DEBUG variable : ``` $ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))' @@ -356,7 +357,7 @@ enabled or disabled. ## Usage in child processes -Due to the way `debug` detects if the output is a TTY or not, colors are not shown in child processes when `stderr` is piped. A solution is to pass the `DEBUG_COLORS=1` environment variable to the child process. +Due to the way `debug` detects if the output is a TTY or not, colors are not shown in child processes when `stderr` is piped. A solution is to pass the `DEBUG_COLORS=1` environment variable to the child process. For example: ```javascript diff --git a/src/node.js b/src/node.js index 715560a4..5dfaac65 100644 --- a/src/node.js +++ b/src/node.js @@ -191,7 +191,11 @@ function getDate() { */ function log(...args) { - return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); + if (exports.inspectOpst.useStdout) { + return process.stdout.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); + } else { + return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); + } } /** diff --git a/test.node.js b/test.node.js index 4cc3c051..3d23df08 100644 --- a/test.node.js +++ b/test.node.js @@ -36,5 +36,22 @@ describe('debug node', () => { assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options); stdErrWriteStub.restore(); }); + + it('calls util.formatWithOptions with inspectOpts (STDOUT)', () => { + debug.enable('*'); + const options = { + hideDate: true, + colors: true, + depth: 10, + showHidden: true, + useStdout: true + }; + Object.assign(debug.inspectOpts, options); + const stdOutWriteStub = sinon.stub(process.stdout, 'write'); + const log = debug('format with inspectOpts'); + log('hello world2'); + assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options); + stdOutWriteStub.restore(); + }); }); });