\href{https://travis-ci.org/visionmedia/debug}{\texttt{ }} \href{https://coveralls.io/github/visionmedia/debug?branch=master}{\texttt{ }} \href{https://visionmedia-community-slackin.now.sh/}{\texttt{ }} \href{\#backers}{\texttt{ }} \href{\#sponsors}{\texttt{ }}



A tiny Java\+Script debugging utility modelled after Node.\+js core\textquotesingle{}s debugging technique. Works in Node.\+js and web browsers.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29573}{}\doxysection{Installation}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29573}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install debug }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29574}{}\doxysection{Usage}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29574}
{\ttfamily debug} exposes a function; simply pass this function the name of your module, and it will return a decorated version of {\ttfamily 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.

Example \href{./examples/node/app.js}{\texttt{ {\itshape app.\+js}}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var debug = require('debug')('http')}
\DoxyCodeLine{  , http = require('http')}
\DoxyCodeLine{  , name = 'My App';}
\DoxyCodeLine{}
\DoxyCodeLine{// fake app}
\DoxyCodeLine{}
\DoxyCodeLine{debug('booting \%o', name);}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(function(req, res)\{}
\DoxyCodeLine{  debug(req.method + ' ' + req.url);}
\DoxyCodeLine{  res.end('hello\(\backslash\)n');}
\DoxyCodeLine{\}).listen(3000, function()\{}
\DoxyCodeLine{  debug('listening');}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{// fake worker of some kind}
\DoxyCodeLine{}
\DoxyCodeLine{require('./worker'); }

\end{DoxyCode}


Example \href{./examples/node/worker.js}{\texttt{ {\itshape worker.\+js}}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var a = require('debug')('worker:a')}
\DoxyCodeLine{  , b = require('debug')('worker:b');}
\DoxyCodeLine{}
\DoxyCodeLine{function work() \{}
\DoxyCodeLine{  a('doing lots of uninteresting work');}
\DoxyCodeLine{  setTimeout(work, Math.random() * 1000);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{work();}
\DoxyCodeLine{}
\DoxyCodeLine{function workb() \{}
\DoxyCodeLine{  b('doing some work');}
\DoxyCodeLine{  setTimeout(workb, Math.random() * 2000);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{workb(); }

\end{DoxyCode}


The {\ttfamily DEBUG} environment variable is then used to enable these based on space or comma-\/delimited names.

Here are some examples\+:

  \hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29575}{}\doxysubsubsection{Windows note}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29575}
On Windows the environment variable is set using the {\ttfamily set} command.


\begin{DoxyCode}{0}
\DoxyCodeLine{set DEBUG=*,-\/not\_this }

\end{DoxyCode}


Note that Power\+Shell uses different syntax to set environment variables.


\begin{DoxyCode}{0}
\DoxyCodeLine{\$env:DEBUG = "{}*,-\/not\_this"{} }

\end{DoxyCode}


Then, run the program to be debugged as usual.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29576}{}\doxysection{Namespace Colors}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29576}
Every debug instance has a color generated for it based on its namespace name. This helps when visually parsing the debug output to identify which debug instance a debug line belongs to.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29577}{}\doxysubsubsection{Node.\+js}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29577}
In Node.\+js, colors are enabled when stderr is a TTY. You also {\itshape should} install the \href{https://npmjs.org/supports-color}{\texttt{ {\ttfamily supports-\/color}}} module alongside debug, otherwise debug will only use a small handful of basic colors.

\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29578}{}\doxysubsubsection{Web Browser}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29578}
Colors are also enabled on \char`\"{}\+Web Inspectors\char`\"{} that understand the {\ttfamily c} formatting option. These are Web\+Kit web inspectors, Firefox (\href{https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/}{\texttt{ since version 31}}) and the Firebug plugin for Firefox (any version).

\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29579}{}\doxysection{Millisecond diff}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29579}
When actively developing an application it can be useful to see when the time spent between one {\ttfamily debug()} call and the next. Suppose for example you invoke {\ttfamily debug()} before requesting a resource, and after as well, the \char`\"{}+\+NNNms\char`\"{} will show you how much time was spent between calls.



When stdout is not a TTY, {\ttfamily Date\#to\+ISOString()} is used, making it more useful for logging the debug information as shown below\+:

\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29580}{}\doxysection{Conventions}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29580}
If you\textquotesingle{}re using this in one or more of your libraries, you {\itshape 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 {\itshape should} prefix them with your library name and use \char`\"{}\+:\char`\"{} to separate features. For example \char`\"{}body\+Parser\char`\"{} from Connect would then be \char`\"{}connect\+:body\+Parser\char`\"{}. If you append a \char`\"{}$\ast$\char`\"{} 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.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29581}{}\doxysection{Wildcards}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29581}
The {\ttfamily $\ast$} character may be used as a wildcard. Suppose for example your library has debuggers named \char`\"{}connect\+:body\+Parser\char`\"{}, \char`\"{}connect\+:compress\char`\"{}, \char`\"{}connect\+:session\char`\"{}, instead of listing all three with {\ttfamily DEBUG=connect\+:body\+Parser,connect\+:compress,connect\+:session}, you may simply do {\ttfamily DEBUG=connect\+:$\ast$}, or to run everything using this module simply use {\ttfamily DEBUG=$\ast$}.

You can also exclude specific debuggers by prefixing them with a \char`\"{}-\/\char`\"{} character. For example, {\ttfamily DEBUG=$\ast$,-\/connect\+:$\ast$} would include all debuggers except those starting with \char`\"{}connect\+:\char`\"{}.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29582}{}\doxysection{Environment Variables}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29582}
When running through Node.\+js, you can set a few environment variables that will change the behavior of the debug logging\+:

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{2}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Name   }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Purpose    }\\\cline{1-2}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Name   }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Purpose    }\\\cline{1-2}
\endhead
{\ttfamily DEBUG}   &Enables/disables specific debugging namespaces.    \\\cline{1-2}
{\ttfamily DEBUG\+\_\+\+HIDE\+\_\+\+DATE}   &Hide date from debug output (non-\/\+TTY).    \\\cline{1-2}
{\ttfamily DEBUG\+\_\+\+COLORS}   &Whether or not to use colors in the debug output.    \\\cline{1-2}
{\ttfamily DEBUG\+\_\+\+DEPTH}   &Object inspection depth.    \\\cline{1-2}
{\ttfamily DEBUG\+\_\+\+SHOW\+\_\+\+HIDDEN}   &Shows hidden properties on inspected objects.   \\\cline{1-2}
\end{longtabu}


{\bfseries{Note\+:}} The environment variables beginning with {\ttfamily DEBUG\+\_\+} end up being converted into an Options object that gets used with {\ttfamily o}/{\ttfamily O} formatters. See the Node.\+js documentation for \href{https://nodejs.org/api/util.html\#util_util_inspect_object_options}{\texttt{ {\ttfamily util.\+inspect()}}} for the complete list.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29583}{}\doxysection{Formatters}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29583}
Debug uses \href{https://wikipedia.org/wiki/Printf_format_string}{\texttt{ printf-\/style}} formatting. Below are the officially supported formatters\+:

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{2}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Formatter   }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Representation    }\\\cline{1-2}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Formatter   }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ Representation    }\\\cline{1-2}
\endhead
{\ttfamily O}   &Pretty-\/print an Object on multiple lines.    \\\cline{1-2}
{\ttfamily o}   &Pretty-\/print an Object all on a single line.    \\\cline{1-2}
{\ttfamily s}   &String.    \\\cline{1-2}
{\ttfamily d}   &Number (both integer and float).    \\\cline{1-2}
{\ttfamily j}   &JSON. Replaced with the string \textquotesingle{}\mbox{[}Circular\mbox{]}\textquotesingle{} if the argument contains circular references.    \\\cline{1-2}
{\ttfamily \%\%}   &Single percent sign (\textquotesingle{}\textquotesingle{}). This does not consume an argument.   \\\cline{1-2}
\end{longtabu}
\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29584}{}\doxysubsection{Custom formatters}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29584}
You can add custom formatters by extending the {\ttfamily debug.\+formatters} object. For example, if you wanted to add support for rendering a Buffer as hex with {\ttfamily h}, you could do something like\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const createDebug = require('debug')}
\DoxyCodeLine{createDebug.formatters.h = (v) => \{}
\DoxyCodeLine{  return v.toString('hex')}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{// …elsewhere}
\DoxyCodeLine{const debug = createDebug('foo')}
\DoxyCodeLine{debug('this is hex: \%h', new Buffer('hello world'))}
\DoxyCodeLine{//   foo this is hex: 68656c6c6f20776f726c6421 +0ms }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29585}{}\doxysection{Browser Support}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29585}
You can build a browser-\/ready script using \href{https://github.com/substack/node-browserify}{\texttt{ browserify}}, or just use the \href{https://wzrd.in/}{\texttt{ browserify-\/as-\/a-\/service}} \href{https://wzrd.in/standalone/debug@latest}{\texttt{ build}}, if you don\textquotesingle{}t want to build it yourself.

Debug\textquotesingle{}s enable state is currently persisted by {\ttfamily local\+Storage}. Consider the situation shown below where you have {\ttfamily worker\+:a} and {\ttfamily worker\+:b}, and wish to debug both. You can enable this using {\ttfamily local\+Storage.\+debug}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{localStorage.debug = 'worker:*' }

\end{DoxyCode}


And then refresh the page.


\begin{DoxyCode}{0}
\DoxyCodeLine{a = debug('worker:a');}
\DoxyCodeLine{b = debug('worker:b');}
\DoxyCodeLine{}
\DoxyCodeLine{setInterval(function()\{}
\DoxyCodeLine{  a('doing some work');}
\DoxyCodeLine{\}, 1000);}
\DoxyCodeLine{}
\DoxyCodeLine{setInterval(function()\{}
\DoxyCodeLine{  b('doing some work');}
\DoxyCodeLine{\}, 1200); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29586}{}\doxysection{Output streams}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29586}
By default {\ttfamily debug} will log to stderr, however this can be configured per-\/namespace by overriding the {\ttfamily log} method\+:

Example \href{./examples/node/stdout.js}{\texttt{ {\itshape stdout.\+js}}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var debug = require('debug');}
\DoxyCodeLine{var error = debug('app:error');}
\DoxyCodeLine{}
\DoxyCodeLine{// by default stderr is used}
\DoxyCodeLine{error('goes to stderr!');}
\DoxyCodeLine{}
\DoxyCodeLine{var log = debug('app:log');}
\DoxyCodeLine{// set this namespace to log via console.log}
\DoxyCodeLine{log.log = console.log.bind(console); // don't forget to bind to console!}
\DoxyCodeLine{log('goes to stdout');}
\DoxyCodeLine{error('still goes to stderr!');}
\DoxyCodeLine{}
\DoxyCodeLine{// set all output to go via console.info}
\DoxyCodeLine{// overrides all per-\/namespace log settings}
\DoxyCodeLine{debug.log = console.info.bind(console);}
\DoxyCodeLine{error('now goes to stdout via console.info');}
\DoxyCodeLine{log('still goes to stdout, but via console.info now'); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29587}{}\doxysection{Checking whether a debug target is enabled}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29587}
After you\textquotesingle{}ve created a debug instance, you can determine whether or not it is enabled by checking the {\ttfamily enabled} property\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const debug = require('debug')('http');}
\DoxyCodeLine{}
\DoxyCodeLine{if (debug.enabled) \{}
\DoxyCodeLine{  // do stuff...}
\DoxyCodeLine{\} }

\end{DoxyCode}


You can also manually toggle this property to force the debug instance to be enabled or disabled.\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29588}{}\doxysection{Authors}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29588}

\begin{DoxyItemize}
\item TJ Holowaychuk
\item Nathan Rajlich
\item Andrew Rhyne
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29589}{}\doxysection{Backers}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29589}
Support us with a monthly donation and help us continue our activities. \mbox{[}\href{https://opencollective.com/debug\#backer}{\texttt{ Become a backer}}\mbox{]}

\href{https://opencollective.com/debug/backer/0/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/1/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/2/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/3/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/4/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/5/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/6/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/7/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/8/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/9/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/10/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/11/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/12/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/13/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/14/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/15/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/16/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/17/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/18/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/19/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/20/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/21/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/22/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/23/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/24/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/25/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/26/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/27/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/28/website}{\texttt{ }} \href{https://opencollective.com/debug/backer/29/website}{\texttt{ }}\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29590}{}\doxysection{Sponsors}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29590}
Become a sponsor and get your logo on our README on Github with a link to your site. \mbox{[}\href{https://opencollective.com/debug\#sponsor}{\texttt{ Become a sponsor}}\mbox{]}

\href{https://opencollective.com/debug/sponsor/0/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/1/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/2/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/3/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/4/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/5/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/6/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/7/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/8/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/9/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/10/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/11/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/12/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/13/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/14/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/15/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/16/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/17/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/18/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/19/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/20/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/21/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/22/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/23/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/24/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/25/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/26/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/27/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/28/website}{\texttt{ }} \href{https://opencollective.com/debug/sponsor/29/website}{\texttt{ }}\hypertarget{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29591}{}\doxysection{License}\label{md_sfcode_node_modules_stylus_node_modules_debug_README_autotoc_md29591}
(The MIT License)

Copyright (c) 2014-\/2017 TJ Holowaychuk $<$\href{mailto:tj@vision-media.ca}{\texttt{ tj@vision-\/media.\+ca}}$>$

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \textquotesingle{}Software\textquotesingle{}), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions\+:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED \textquotesingle{}AS IS\textquotesingle{}, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 