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