Title: util.debuglog callback argument behavior and purpose appears to be documented incorrectly · Issue #56676 · nodejs/node · GitHub
Open Graph Title: util.debuglog callback argument behavior and purpose appears to be documented incorrectly · Issue #56676 · nodejs/node
X Title: util.debuglog callback argument behavior and purpose appears to be documented incorrectly · Issue #56676 · nodejs/node
Description: Affected URL(s) https://nodejs.org/docs/latest/api/util.html#utildebuglogsection-callback Description of the problem The usage example of the callback function for util.debuglog is not sufficiently documented imho. Moreover, when looking...
Open Graph Description: Affected URL(s) https://nodejs.org/docs/latest/api/util.html#utildebuglogsection-callback Description of the problem The usage example of the callback function for util.debuglog is not sufficiently...
X Description: Affected URL(s) https://nodejs.org/docs/latest/api/util.html#utildebuglogsection-callback Description of the problem The usage example of the callback function for util.debuglog is not sufficiently...
Opengraph URL: https://github.com/nodejs/node/issues/56676
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"util.debuglog callback argument behavior and purpose appears to be documented incorrectly","articleBody":"### Affected URL(s)\n\nhttps://nodejs.org/docs/latest/api/util.html#utildebuglogsection-callback\n\n### Description of the problem\n\nThe usage example of the callback function for `util.debuglog` is not sufficiently documented imho.\nMoreover, when looking at the code, it also seems to even work differently than described. \n\nThe example for the callback does the following:\n```javascript\nconst util = require('node:util');\nlet debuglog = util.debuglog('internals', (debug) =\u003e {\n // Replace with a logging function that optimizes out\n // testing if the section is enabled\n debuglog = debug;\n});\n```\n\nWhat debuglog effectively does is:\n```javascript\nfunction debuglog(set, cb){\n realLogFunction = (...args){\n init() //sets up the set name and an enabled boolean value\n\n // creates a noop function when enabled=false\n realLogFunction = debugLogImpl(enabled) \n if(typeof cb == \"function\"){\n // this is the part where the documentation is lacking\n cb(realLogFunction) //code depends on side-effects of the callback?\n }\n // this line is only effective the very first time, when still in the factory\n // likely to not 'swallow' the first log line that lazily inits the real logger\n realLogFunction(...args)\n }\n\n //left out for brewity, but works on the same principle as realLogFunction\n let innerEnabledCheck ()=\u003e{...}\n \n logDelegate = (...) =\u003e {\n //some switch on argument length, but effectively always results in the next line, with a varying\n //amount of arguments passed on.\n realLogFunction(args)\n }\n logDelegate.enabled = innerEnabledCheck() //left out for brewity\n return logDelegate;\n}\n```\n\nThe first time `logDelegate` is called, the enabled test is already optimized out and `logDelegate` will call the now-changed `realLogFunction` generated from `debugLogImpl`.\nWhat actually happens when the callback is used as in the documentation's example, is that the outside variable, (`debuglog` in the example) which points to `logDelegate`, will be replaced by `realLogFunction`.\n\nThus, what is 'optimized out' is the switch in `logDelegate` when the callback is used as shown in the example, but at the price of also losing the `enabled` getter. 'Testing if the section is enabled' is always automatically and implicitly optimized out in the inner factory method.","author":{"url":"https://github.com/GB609","@type":"Person","name":"GB609"},"datePublished":"2025-01-21T10:41:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/56676/node/issues/56676"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5f28bf7f-dd2d-6f8a-0f40-f998f1561f33 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C2A0:9B927:788AFE:AC8553:6A4CAE7D |
| html-safe-nonce | e3edcb7fce02499b0f72251304a05d42c211f808a0f58422bee4aa17d4bdcbf2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMkEwOjlCOTI3Ojc4OEFGRTpBQzg1NTM6NkE0Q0FFN0QiLCJ2aXNpdG9yX2lkIjoiNjM0MTQ1ODQwMTg4NTQ2NjIzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7fc0b1ea6c0d348d5b46e366ab3f53c02b12872237e47bbb2f53d5e4f6881f18 |
| hovercard-subject-tag | issue:2801416051 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/56676/issue_layout |
| twitter:image | https://opengraph.githubassets.com/476e58d434de72d4923f7e82928e19a4cb06450226b239ae010d68323e7e12d1/nodejs/node/issues/56676 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/476e58d434de72d4923f7e82928e19a4cb06450226b239ae010d68323e7e12d1/nodejs/node/issues/56676 |
| og:image:alt | Affected URL(s) https://nodejs.org/docs/latest/api/util.html#utildebuglogsection-callback Description of the problem The usage example of the callback function for util.debuglog is not sufficiently... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GB609 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ae90d426644ca15e89bacceb72e51f4e9dbf85f7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width