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

doc: util.debuglog callback #33856

Closed
wants to merge 7 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion doc/api/util.md
Expand Up @@ -68,13 +68,15 @@ callbackFunction((err, ret) => {
});
```

## `util.debuglog(section)`
## `util.debuglog(section[, callback])`
<!-- YAML
added: v0.11.3
-->

* `section` {string} A string identifying the portion of the application for
which the `debuglog` function is being created.
* `callback` {Function} A callback invoked the first time the logging function
jasnell marked this conversation as resolved.
Show resolved Hide resolved
is called with a function argument that is a more optimized logging function.
* Returns: {Function} The logging function

The `util.debuglog()` method is used to create a function that conditionally
Expand Down Expand Up @@ -119,6 +121,29 @@ FOO-BAR 3257: hi there, it's foo-bar [2333]
Multiple comma-separated `section` names may be specified in the `NODE_DEBUG`
environment variable: `NODE_DEBUG=fs,net,tls`.

The optional `callback` argument can be used to perform logic depending on if
a section is enabled.

```js
const util = require('util');
let makeInternalsPublic = false;
bmeck marked this conversation as resolved.
Show resolved Hide resolved
const debug = util.debuglog('internals', (debug) => {
makeInternalsPublic = debug !== null;
});
let counter = 1;
debug('exposing internals, do not use them as a public API');
module.exports = {
internal: makeInternalsPublic ? {
setCounter(newValue) {
counter = newValue;
}
} : null,
nextValue() {
return counter++;
}
};
```

bmeck marked this conversation as resolved.
Show resolved Hide resolved
## `util.deprecate(fn, msg[, code])`
<!-- YAML
added: v0.8.0
Expand Down