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)
5 <img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
7 A tiny JavaScript debugging utility modelled after Node.js core's debugging
8 technique. Works in Node.js and web browsers.
18 `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.
20 Example [_app.js_](./examples/node/app.js):
23 var debug = require('debug')('http')
24 , http = require('http')
29 debug('booting %o', name);
31 http.createServer(function(req, res){
32 debug(req.method + ' ' + req.url);
34 }).listen(3000, function(){
38 // fake worker of some kind
43 Example [_worker.js_](./examples/node/worker.js):
46 var a = require('debug')('worker:a')
47 , b = require('debug')('worker:b');
50 a('doing lots of uninteresting work');
51 setTimeout(work, Math.random() * 1000);
58 setTimeout(workb, Math.random() * 2000);
64 The `DEBUG` environment variable is then used to enable these based on space or
65 comma-delimited names.
67 Here are some examples:
69 <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">
70 <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">
71 <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">
73 #### Windows command prompt notes
77 On Windows the environment variable is set using the `set` command.
86 set DEBUG=* & node app.js
89 ##### PowerShell (VS Code default)
91 PowerShell uses different syntax to set environment variables.
94 $env:DEBUG = "*,-not_this"
100 $env:DEBUG='app';node app.js
103 Then, run the program to be debugged as usual.
107 "windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",
112 Every debug instance has a color generated for it based on its namespace name.
113 This helps when visually parsing the debug output to identify which debug instance
114 a debug line belongs to.
118 In Node.js, colors are enabled when stderr is a TTY. You also _should_ install
119 the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,
120 otherwise debug will only use a small handful of basic colors.
122 <img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">
126 Colors are also enabled on "Web Inspectors" that understand the `%c` formatting
127 option. These are WebKit web inspectors, Firefox ([since version
128 31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))
129 and the Firebug plugin for Firefox (any version).
131 <img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">
136 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.
138 <img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
140 When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:
142 <img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">
147 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.
151 The `*` character may be used as a wildcard. Suppose for example your library has
152 debuggers named "connect:bodyParser", "connect:compress", "connect:session",
153 instead of listing all three with
154 `DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do
155 `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
157 You can also exclude specific debuggers by prefixing them with a "-" character.
158 For example, `DEBUG=*,-connect:*` would include all debuggers except those
159 starting with "connect:".
161 ## Environment Variables
163 When running through Node.js, you can set a few environment variables that will
164 change the behavior of the debug logging:
167 |-----------|-------------------------------------------------|
168 | `DEBUG` | Enables/disables specific debugging namespaces. |
169 | `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
170 | `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
171 | `DEBUG_DEPTH` | Object inspection depth. |
172 | `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
175 __Note:__ The environment variables beginning with `DEBUG_` end up being
176 converted into an Options object that gets used with `%o`/`%O` formatters.
177 See the Node.js documentation for
178 [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)
179 for the complete list.
183 Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
184 Below are the officially supported formatters:
186 | Formatter | Representation |
187 |-----------|----------------|
188 | `%O` | Pretty-print an Object on multiple lines. |
189 | `%o` | Pretty-print an Object all on a single line. |
191 | `%d` | Number (both integer and float). |
192 | `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
193 | `%%` | Single percent sign ('%'). This does not consume an argument. |
196 ### Custom formatters
198 You can add custom formatters by extending the `debug.formatters` object.
199 For example, if you wanted to add support for rendering a Buffer as hex with
200 `%h`, you could do something like:
203 const createDebug = require('debug')
204 createDebug.formatters.h = (v) => {
205 return v.toString('hex')
209 const debug = createDebug('foo')
210 debug('this is hex: %h', new Buffer('hello world'))
211 // foo this is hex: 68656c6c6f20776f726c6421 +0ms
217 You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),
218 or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),
219 if you don't want to build it yourself.
221 Debug's enable state is currently persisted by `localStorage`.
222 Consider the situation shown below where you have `worker:a` and `worker:b`,
223 and wish to debug both. You can enable this using `localStorage.debug`:
226 localStorage.debug = 'worker:*'
229 And then refresh the page.
232 a = debug('worker:a');
233 b = debug('worker:b');
235 setInterval(function(){
236 a('doing some work');
239 setInterval(function(){
240 b('doing some work');
247 By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:
249 Example [_stdout.js_](./examples/node/stdout.js):
252 var debug = require('debug');
253 var error = debug('app:error');
255 // by default stderr is used
256 error('goes to stderr!');
258 var log = debug('app:log');
259 // set this namespace to log via console.log
260 log.log = console.log.bind(console); // don't forget to bind to console!
261 log('goes to stdout');
262 error('still goes to stderr!');
264 // set all output to go via console.info
265 // overrides all per-namespace log settings
266 debug.log = console.info.bind(console);
267 error('now goes to stdout via console.info');
268 log('still goes to stdout, but via console.info now');
272 You can simply extend debugger
274 const log = require('debug')('auth');
276 //creates new debug instance with extended namespace
277 const logSign = log.extend('sign');
278 const logLogin = log.extend('login');
280 log('hello'); // auth hello
281 logSign('hello'); //auth:sign hello
282 logLogin('hello'); //auth:login hello
287 You can also enable debug dynamically by calling the `enable()` method :
290 let debug = require('debug');
292 console.log(1, debug.enabled('test'));
294 debug.enable('test');
295 console.log(2, debug.enabled('test'));
298 console.log(3, debug.enabled('test'));
311 `namespaces` can include modes separated by a colon and wildcards.
313 Note that calling `enable()` completely overrides previously set DEBUG variable :
316 $ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
320 ## Checking whether a debug target is enabled
322 After you've created a debug instance, you can determine whether or not it is
323 enabled by checking the `enabled` property:
326 const debug = require('debug')('http');
333 You can also manually toggle this property to force the debug instance to be
345 Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]
347 <a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>
348 <a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>
349 <a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>
350 <a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>
351 <a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>
352 <a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>
353 <a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>
354 <a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>
355 <a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>
356 <a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>
357 <a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>
358 <a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>
359 <a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>
360 <a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>
361 <a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>
362 <a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>
363 <a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>
364 <a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>
365 <a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>
366 <a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>
367 <a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>
368 <a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>
369 <a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>
370 <a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>
371 <a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>
372 <a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>
373 <a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>
374 <a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>
375 <a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>
376 <a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>
381 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)]
383 <a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>
384 <a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>
385 <a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>
386 <a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>
387 <a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>
388 <a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>
389 <a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>
390 <a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>
391 <a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>
392 <a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>
393 <a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>
394 <a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>
395 <a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>
396 <a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>
397 <a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>
398 <a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>
399 <a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>
400 <a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>
401 <a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>
402 <a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>
403 <a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>
404 <a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>
405 <a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>
406 <a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>
407 <a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>
408 <a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>
409 <a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>
410 <a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>
411 <a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>
412 <a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>
418 Copyright (c) 2014-2017 TJ Holowaychuk <tj@vision-media.ca>
420 Permission is hereby granted, free of charge, to any person obtaining
421 a copy of this software and associated documentation files (the
422 'Software'), to deal in the Software without restriction, including
423 without limitation the rights to use, copy, modify, merge, publish,
424 distribute, sublicense, and/or sell copies of the Software, and to
425 permit persons to whom the Software is furnished to do so, subject to
426 the following conditions:
428 The above copyright notice and this permission notice shall be
429 included in all copies or substantial portions of the Software.
431 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
432 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
433 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
434 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
435 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
436 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
437 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.