Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to log to STDOUT #965

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});