Skip to content

Commit 9481d46

Browse files
committed
Add option to log to STDOUT
Signed-off-by: Gerard Hickey <hickey@kinetic-compute.com>
1 parent 5464bdd commit 9481d46

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ change the behavior of the debug logging:
170170
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
171171
| `DEBUG_DEPTH` | Object inspection depth. |
172172
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
173+
| `DEBUG_USE_STDOUT` | Use STDOUT instead of STDERR for messages |
173174

174175

175176
__Note:__ The environment variables beginning with `DEBUG_` end up being
@@ -272,7 +273,7 @@ log('still goes to stdout, but via console.info now');
272273
```
273274

274275
## Extend
275-
You can simply extend debugger
276+
You can simply extend debugger
276277
```js
277278
const log = require('debug')('auth');
278279

@@ -302,18 +303,18 @@ console.log(3, debug.enabled('test'));
302303

303304
```
304305

305-
print :
306+
print :
306307
```
307308
1 false
308309
2 true
309310
3 false
310311
```
311312

312-
Usage :
313-
`enable(namespaces)`
313+
Usage :
314+
`enable(namespaces)`
314315
`namespaces` can include modes separated by a colon and wildcards.
315-
316-
Note that calling `enable()` completely overrides previously set DEBUG variable :
316+
317+
Note that calling `enable()` completely overrides previously set DEBUG variable :
317318

318319
```
319320
$ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
@@ -356,7 +357,7 @@ enabled or disabled.
356357

357358
## Usage in child processes
358359

359-
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.
360+
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.
360361
For example:
361362

362363
```javascript

src/node.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ function getDate() {
191191
*/
192192

193193
function log(...args) {
194-
return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
194+
if (exports.inspectOpst.useStdout) {
195+
return process.stdout.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
196+
} else {
197+
return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
198+
}
195199
}
196200

197201
/**

test.node.js

+17
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,22 @@ describe('debug node', () => {
3636
assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options);
3737
stdErrWriteStub.restore();
3838
});
39+
40+
it('calls util.formatWithOptions with inspectOpts (STDOUT)', () => {
41+
debug.enable('*');
42+
const options = {
43+
hideDate: true,
44+
colors: true,
45+
depth: 10,
46+
showHidden: true,
47+
useStdout: true
48+
};
49+
Object.assign(debug.inspectOpts, options);
50+
const stdOutWriteStub = sinon.stub(process.stdout, 'write');
51+
const log = debug('format with inspectOpts');
52+
log('hello world2');
53+
assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options);
54+
stdOutWriteStub.restore();
55+
});
3956
});
4057
});

0 commit comments

Comments
 (0)