UNPKG

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