├── LICENSE ├── README.md ├── debuglog.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to 5 | deal in the Software without restriction, including without limitation the 6 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # debuglog - backport of util.debuglog() from node v0.11 2 | 3 | To facilitate using the `util.debuglog()` function that will be available when 4 | node v0.12 is released now, this is a copy extracted from the source. 5 | 6 | ## require('debuglog') 7 | 8 | Return `util.debuglog`, if it exists, otherwise it will return an internal copy 9 | of the implementation from node v0.11. 10 | 11 | ## debuglog(section) 12 | 13 | * `section` {String} The section of the program to be debugged 14 | * Returns: {Function} The logging function 15 | 16 | This is used to create a function which conditionally writes to stderr 17 | based on the existence of a `NODE_DEBUG` environment variable. If the 18 | `section` name appears in that environment variable, then the returned 19 | function will be similar to `console.error()`. If not, then the 20 | returned function is a no-op. 21 | 22 | For example: 23 | 24 | ```javascript 25 | var debuglog = util.debuglog('foo'); 26 | 27 | var bar = 123; 28 | debuglog('hello from foo [%d]', bar); 29 | ``` 30 | 31 | If this program is run with `NODE_DEBUG=foo` in the environment, then 32 | it will output something like: 33 | 34 | FOO 3245: hello from foo [123] 35 | 36 | where `3245` is the process id. If it is not run with that 37 | environment variable set, then it will not print anything. 38 | 39 | You may separate multiple `NODE_DEBUG` environment variables with a 40 | comma. For example, `NODE_DEBUG=fs,net,tls`. 41 | -------------------------------------------------------------------------------- /debuglog.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | module.exports = (util && util.debuglog) || debuglog; 4 | 5 | var debugs = {}; 6 | var debugEnviron = process.env.NODE_DEBUG || ''; 7 | 8 | function debuglog(set) { 9 | set = set.toUpperCase(); 10 | if (!debugs[set]) { 11 | if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { 12 | var pid = process.pid; 13 | debugs[set] = function() { 14 | var msg = util.format.apply(exports, arguments); 15 | console.error('%s %d: %s', set, pid, msg); 16 | }; 17 | } else { 18 | debugs[set] = function() {}; 19 | } 20 | } 21 | return debugs[set]; 22 | }; 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "debuglog", 3 | "version": "1.0.1", 4 | "description": "backport of util.debuglog from node v0.11", 5 | "license": "MIT", 6 | "main": "debuglog.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/sam-github/node-debuglog.git" 10 | }, 11 | "author": { 12 | "name": "Sam Roberts", 13 | "email": "sam@strongloop.com" 14 | }, 15 | "engines": { 16 | "node": "*" 17 | }, 18 | "browser": { 19 | "util": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------