Skip to content

Commit

Permalink
Add option to log to STDOUT
Browse files Browse the repository at this point in the history
Signed-off-by: Gerard Hickey <[email protected]>
  • Loading branch information
hickey committed Jun 5, 2024
1 parent 5464bdd commit 27d567f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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"))'
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ function getDate() {
*/

function log(...args) {
return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
if (exports.inspectOpts.useStdout) {
return process.stdout.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
} else {
return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 27d567f

Please sign in to comment.