Skip to content
Closed
Changes from 1 commit
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
Next Next commit
doc: util.debuglog callback
  • Loading branch information
bmeck committed Jun 12, 2020
commit 90cd514a409c85db91bcc2739d0ded5b6082a69a
28 changes: 27 additions & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ 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 that is called with `null` if the section is
disabled or the logging function if enabled the first time the logging function
is called.
* Returns: {Function} The logging function

The `util.debuglog()` method is used to create a function that conditionally
Expand Down Expand Up @@ -119,6 +122,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;
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++;
}
};
```

## `util.deprecate(fn, msg[, code])`
<!-- YAML
added: v0.8.0
Expand Down