You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
21 KiB

4 years ago
  1. # debug
  2. [![Build Status](https://travis-ci.org/visionmedia/debug.svg?branch=master)](https://travis-ci.org/visionmedia/debug) [![Coverage Status](https://coveralls.io/repos/github/visionmedia/debug/badge.svg?branch=master)](https://coveralls.io/github/visionmedia/debug?branch=master) [![Slack](https://visionmedia-community-slackin.now.sh/badge.svg)](https://visionmedia-community-slackin.now.sh/) [![OpenCollective](https://opencollective.com/debug/backers/badge.svg)](#backers)
  3. [![OpenCollective](https://opencollective.com/debug/sponsors/badge.svg)](#sponsors)
  4. <img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
  5. A tiny JavaScript debugging utility modelled after Node.js core's debugging
  6. technique. Works in Node.js and web browsers.
  7. ## Installation
  8. ```bash
  9. $ npm install debug
  10. ```
  11. ## Usage
  12. `debug` exposes a function; simply pass this function the name of your module, and it will return a decorated version of `console.error` for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.
  13. Example [_app.js_](./examples/node/app.js):
  14. ```js
  15. var debug = require('debug')('http')
  16. , http = require('http')
  17. , name = 'My App';
  18. // fake app
  19. debug('booting %o', name);
  20. http.createServer(function(req, res){
  21. debug(req.method + ' ' + req.url);
  22. res.end('hello\n');
  23. }).listen(3000, function(){
  24. debug('listening');
  25. });
  26. // fake worker of some kind
  27. require('./worker');
  28. ```
  29. Example [_worker.js_](./examples/node/worker.js):
  30. ```js
  31. var a = require('debug')('worker:a')
  32. , b = require('debug')('worker:b');
  33. function work() {
  34. a('doing lots of uninteresting work');
  35. setTimeout(work, Math.random() * 1000);
  36. }
  37. work();
  38. function workb() {
  39. b('doing some work');
  40. setTimeout(workb, Math.random() * 2000);
  41. }
  42. workb();
  43. ```
  44. The `DEBUG` environment variable is then used to enable these based on space or
  45. comma-delimited names.
  46. Here are some examples:
  47. <img width="647" alt="screen shot 2017-08-08 at 12 53 04 pm" src="https://user-images.githubusercontent.com/71256/29091703-a6302cdc-7c38-11e7-8304-7c0b3bc600cd.png">
  48. <img width="647" alt="screen shot 2017-08-08 at 12 53 38 pm" src="https://user-images.githubusercontent.com/71256/29091700-a62a6888-7c38-11e7-800b-db911291ca2b.png">
  49. <img width="647" alt="screen shot 2017-08-08 at 12 53 25 pm" src="https://user-images.githubusercontent.com/71256/29091701-a62ea114-7c38-11e7-826a-2692bedca740.png">
  50. #### Windows command prompt notes
  51. ##### CMD
  52. On Windows the environment variable is set using the `set` command.
  53. ```cmd
  54. set DEBUG=*,-not_this
  55. ```
  56. Example:
  57. ```cmd
  58. set DEBUG=* & node app.js
  59. ```
  60. ##### PowerShell (VS Code default)
  61. PowerShell uses different syntax to set environment variables.
  62. ```cmd
  63. $env:DEBUG = "*,-not_this"
  64. ```
  65. Example:
  66. ```cmd
  67. $env:DEBUG='app';node app.js
  68. ```
  69. Then, run the program to be debugged as usual.
  70. npm script example:
  71. ```js
  72. "windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",
  73. ```
  74. ## Namespace Colors
  75. Every debug instance has a color generated for it based on its namespace name.
  76. This helps when visually parsing the debug output to identify which debug instance
  77. a debug line belongs to.
  78. #### Node.js
  79. In Node.js, colors are enabled when stderr is a TTY. You also _should_ install
  80. the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,
  81. otherwise debug will only use a small handful of basic colors.
  82. <img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">
  83. #### Web Browser
  84. Colors are also enabled on "Web Inspectors" that understand the `%c` formatting
  85. option. These are WebKit web inspectors, Firefox ([since version
  86. 31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))
  87. and the Firebug plugin for Firefox (any version).
  88. <img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">
  89. ## Millisecond diff
  90. When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
  91. <img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
  92. When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:
  93. <img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">
  94. ## Conventions
  95. If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable. You can then use it for normal output as well as debug output.
  96. ## Wildcards
  97. The `*` character may be used as a wildcard. Suppose for example your library has
  98. debuggers named "connect:bodyParser", "connect:compress", "connect:session",
  99. instead of listing all three with
  100. `DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do
  101. `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
  102. You can also exclude specific debuggers by prefixing them with a "-" character.
  103. For example, `DEBUG=*,-connect:*` would include all debuggers except those
  104. starting with "connect:".
  105. ## Environment Variables
  106. When running through Node.js, you can set a few environment variables that will
  107. change the behavior of the debug logging:
  108. | Name | Purpose |
  109. |-----------|-------------------------------------------------|
  110. | `DEBUG` | Enables/disables specific debugging namespaces. |
  111. | `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
  112. | `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
  113. | `DEBUG_DEPTH` | Object inspection depth. |
  114. | `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
  115. __Note:__ The environment variables beginning with `DEBUG_` end up being
  116. converted into an Options object that gets used with `%o`/`%O` formatters.
  117. See the Node.js documentation for
  118. [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)
  119. for the complete list.
  120. ## Formatters
  121. Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
  122. Below are the officially supported formatters:
  123. | Formatter | Representation |
  124. |-----------|----------------|
  125. | `%O` | Pretty-print an Object on multiple lines. |
  126. | `%o` | Pretty-print an Object all on a single line. |
  127. | `%s` | String. |
  128. | `%d` | Number (both integer and float). |
  129. | `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
  130. | `%%` | Single percent sign ('%'). This does not consume an argument. |
  131. ### Custom formatters
  132. You can add custom formatters by extending the `debug.formatters` object.
  133. For example, if you wanted to add support for rendering a Buffer as hex with
  134. `%h`, you could do something like:
  135. ```js
  136. const createDebug = require('debug')
  137. createDebug.formatters.h = (v) => {
  138. return v.toString('hex')
  139. }
  140. // …elsewhere
  141. const debug = createDebug('foo')
  142. debug('this is hex: %h', new Buffer('hello world'))
  143. // foo this is hex: 68656c6c6f20776f726c6421 +0ms
  144. ```
  145. ## Browser Support
  146. You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),
  147. or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),
  148. if you don't want to build it yourself.
  149. Debug's enable state is currently persisted by `localStorage`.
  150. Consider the situation shown below where you have `worker:a` and `worker:b`,
  151. and wish to debug both. You can enable this using `localStorage.debug`:
  152. ```js
  153. localStorage.debug = 'worker:*'
  154. ```
  155. And then refresh the page.
  156. ```js
  157. a = debug('worker:a');
  158. b = debug('worker:b');
  159. setInterval(function(){
  160. a('doing some work');
  161. }, 1000);
  162. setInterval(function(){
  163. b('doing some work');
  164. }, 1200);
  165. ```
  166. ## Output streams
  167. By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:
  168. Example [_stdout.js_](./examples/node/stdout.js):
  169. ```js
  170. var debug = require('debug');
  171. var error = debug('app:error');
  172. // by default stderr is used
  173. error('goes to stderr!');
  174. var log = debug('app:log');
  175. // set this namespace to log via console.log
  176. log.log = console.log.bind(console); // don't forget to bind to console!
  177. log('goes to stdout');
  178. error('still goes to stderr!');
  179. // set all output to go via console.info
  180. // overrides all per-namespace log settings
  181. debug.log = console.info.bind(console);
  182. error('now goes to stdout via console.info');
  183. log('still goes to stdout, but via console.info now');
  184. ```
  185. ## Extend
  186. You can simply extend debugger
  187. ```js
  188. const log = require('debug')('auth');
  189. //creates new debug instance with extended namespace
  190. const logSign = log.extend('sign');
  191. const logLogin = log.extend('login');
  192. log('hello'); // auth hello
  193. logSign('hello'); //auth:sign hello
  194. logLogin('hello'); //auth:login hello
  195. ```
  196. ## Set dynamically
  197. You can also enable debug dynamically by calling the `enable()` method :
  198. ```js
  199. let debug = require('debug');
  200. console.log(1, debug.enabled('test'));
  201. debug.enable('test');
  202. console.log(2, debug.enabled('test'));
  203. debug.disable();
  204. console.log(3, debug.enabled('test'));
  205. ```
  206. print :
  207. ```
  208. 1 false
  209. 2 true
  210. 3 false
  211. ```
  212. Usage :
  213. `enable(namespaces)`
  214. `namespaces` can include modes separated by a colon and wildcards.
  215. Note that calling `enable()` completely overrides previously set DEBUG variable :
  216. ```
  217. $ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
  218. => false
  219. ```
  220. `disable()`
  221. Will disable all namespaces. The functions returns the namespaces currently
  222. enabled (and skipped). This can be useful if you want to disable debugging
  223. temporarily without knowing what was enabled to begin with.
  224. For example:
  225. ```js
  226. let debug = require('debug');
  227. debug.enable('foo:*,-foo:bar');
  228. let namespaces = debug.disable();
  229. debug.enable(namespaces);
  230. ```
  231. Note: There is no guarantee that the string will be identical to the initial
  232. enable string, but semantically they will be identical.
  233. ## Checking whether a debug target is enabled
  234. After you've created a debug instance, you can determine whether or not it is
  235. enabled by checking the `enabled` property:
  236. ```javascript
  237. const debug = require('debug')('http');
  238. if (debug.enabled) {
  239. // do stuff...
  240. }
  241. ```
  242. You can also manually toggle this property to force the debug instance to be
  243. enabled or disabled.
  244. ## Authors
  245. - TJ Holowaychuk
  246. - Nathan Rajlich
  247. - Andrew Rhyne
  248. ## Backers
  249. Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]
  250. <a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>
  251. <a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>
  252. <a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>
  253. <a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>
  254. <a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>
  255. <a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>
  256. <a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>
  257. <a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>
  258. <a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>
  259. <a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>
  260. <a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>
  261. <a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>
  262. <a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>
  263. <a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>
  264. <a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>
  265. <a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>
  266. <a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>
  267. <a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>
  268. <a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>
  269. <a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>
  270. <a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>
  271. <a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>
  272. <a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>
  273. <a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>
  274. <a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>
  275. <a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>
  276. <a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>
  277. <a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>
  278. <a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>
  279. <a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>
  280. ## Sponsors
  281. Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/debug#sponsor)]
  282. <a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>
  283. <a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>
  284. <a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>
  285. <a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>
  286. <a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>
  287. <a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>
  288. <a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>
  289. <a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>
  290. <a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>
  291. <a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>
  292. <a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>
  293. <a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>
  294. <a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>
  295. <a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>
  296. <a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>
  297. <a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>
  298. <a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>
  299. <a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>
  300. <a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>
  301. <a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>
  302. <a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>
  303. <a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>
  304. <a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>
  305. <a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>
  306. <a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>
  307. <a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>
  308. <a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>
  309. <a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>
  310. <a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>
  311. <a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>
  312. ## License
  313. (The MIT License)
  314. Copyright (c) 2014-2017 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
  315. Permission is hereby granted, free of charge, to any person obtaining
  316. a copy of this software and associated documentation files (the
  317. 'Software'), to deal in the Software without restriction, including
  318. without limitation the rights to use, copy, modify, merge, publish,
  319. distribute, sublicense, and/or sell copies of the Software, and to
  320. permit persons to whom the Software is furnished to do so, subject to
  321. the following conditions:
  322. The above copyright notice and this permission notice shall be
  323. included in all copies or substantial portions of the Software.
  324. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  325. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  326. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  327. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  328. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  329. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  330. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.