https://www.npmjs.com/package/debug
2018.1.18 星期四 17:20
一 Install
$ npm install debug
二 Usage
2.1
var debug = require('debug')('http')
http = require('http')
debug('listening');
var a = require('debug')('worker:a')
, b = require('debug')('worker:b');
function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}
2.2 Windows
set DEBUG=*,-not_this
三 Namespace Colors
3.1 Node.js
colors are enabled when stderr is a TTY.
install the supports-color module alongside debug
// Question:?TTF
3.2 Web Browser
四 Millisecond diff
When stdout is not a TTY, Date#toISOString() is used
// Question:?TTF
五 Conventions
(公约、惯例、常规、etc)
六 Wildcards
(通配符) * -
七 Environment Variables
name | purpose |
---|---|
DEBUG | Enables/disables specific debugging namespaces. |
DEBUG_HIDE_DATE | Hide date from debug output (non-TTY). |
DEBUG_COLORS | Whether or not to use colors in the debug output. |
DEBUG_DEPTH | Object inspection depth. |
DEBUG_SHOW_HIDDEN | Shows hidden properties on inspected objects. |
八 Formatters
8.1
Debug uses printf-style formatting. Below are the officially supported formatters:
formatter | representation |
---|---|
%O | Pretty-print an Object on multiple lines. |
%o | Pretty-print an Object all on a single line. |
%s | String. |
%d | Number (both integer and float). |
%j | JSON. Replaced with the string ‘[Circular]’ if the argument contains circular references. |
%% | Single percent sign (‘%’). This does not consume an argument. |
8.2 Custom formatters
const createDebug = require('debug')
createDebug.formatters.h = (v) => {
return v.toString('hex')
}
// …elsewhere
const debug = createDebug('foo')
debug('this is hex: %h', new Buffer('hello world'))
// foo this is hex: 68656c6c6f20776f726c6421 +0ms
九 Browser Support
You can build a browser-ready script using browserify, or just use the browserify-as-a-service build, if you don’t want to build it yourself.
localStorage.debug = 'worker:*'
a = debug('worker:a');
十 Output streams
var debug = require('debug');
var error = debug('app:error');
// by default stderr is used
error('goes to stderr!');
var log = debug('app:log');
// set this namespace to log via console.log
log.log = console.log.bind(console); // don't forget to bind to console!
log('goes to stdout');
error('still goes to stderr!');
// set all output to go via console.info
// overrides all per-namespace log settings
debug.log = console.info.bind(console);
error('now goes to stdout via console.info');
log('still goes to stdout, but via console.info now');
// TODO:目前,尚不清楚,没有那么复杂的应用
一 Checking whether a debug target is enabled
const debug = require('debug')('http');
if (debug.enabled) {
// do stuff...
}
Authors
Backers
Sponsors
License
(The MIT License)
17:50
学习总结:
快速学习,能够马上入手。
文档也就一页。
如需要深度掌握(有实际需求)再来学习