debug-logger
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -6,28 +6,35 @@ 'use strict'; | ||
exports = module.exports = debugLogger; | ||
exports.getForeColor = getForeColor; | ||
exports.getBackColor = getBackColor; | ||
exports.inspectOptions = {}; | ||
var DEBUG_NAMESPACE = ':debug'; | ||
var RED = '\x1b[31m'; | ||
var GREEN = '\x1b[32m'; | ||
var YELLOW = '\x1b[33m'; | ||
var BLUE = '\x1b[34m'; | ||
var RESET = '\x1b[0m'; | ||
exports.colors = { | ||
black : 0, | ||
red : 1, | ||
green : 2, | ||
yellow : 3, | ||
blue : 4, | ||
magenta : 5, | ||
cyan : 6, | ||
white : 7 | ||
}; | ||
exports.colorReset = '\x1b[0m'; | ||
exports.levels = { | ||
debug : { | ||
color : BLUE, | ||
color : getForeColor('blue'), | ||
prefix : 'DEBUG ', | ||
debugLogger : true | ||
namespaceSuffix : ':debug' | ||
}, | ||
info : { | ||
color : GREEN, | ||
color : getForeColor('green'), | ||
prefix : ' INFO ' | ||
}, | ||
warn : { | ||
color : YELLOW, | ||
color : getForeColor('yellow'), | ||
prefix : ' WARN ' | ||
}, | ||
error : { | ||
color : RED, | ||
color : getForeColor('red'), | ||
prefix : ' ERROR ' | ||
@@ -59,3 +66,3 @@ } | ||
return errorStrings; | ||
}; | ||
} | ||
@@ -66,28 +73,38 @@ function getPadding(size){ | ||
function getForeColor(color){ | ||
return '\x1b[' + (30 + exports.colors[color]) + 'm'; | ||
} | ||
function getBackColor(color){ | ||
return '\x1b[' + (40 + exports.colors[color]) + 'm'; | ||
} | ||
function debugLogger(namespace) { | ||
var levels = exports.levels; | ||
var defaultPadding = '\n' + getPadding(2); | ||
var log = vmDebug(namespace); | ||
var debug = vmDebug(namespace + DEBUG_NAMESPACE); | ||
var defaultPadding = '\n' + getPadding(2); | ||
var debugLoggers = { 'default': log }; | ||
var logger = { | ||
logger : log, | ||
debugLogger : debug, | ||
isEnabled : log.enabled, | ||
isDebugEnabled : debug.enabled | ||
}; | ||
var levels = exports.levels; | ||
var logger = {}; | ||
Object.keys(levels).forEach(function(level) { | ||
var levelLog = levels[level].debugLogger ? debug : log; | ||
var loggerNamespaceSuffix = levels[level].namespaceSuffix ? levels[level].namespaceSuffix : 'default'; | ||
if(!debugLoggers[loggerNamespaceSuffix]){ | ||
debugLoggers[loggerNamespaceSuffix] = vmDebug(namespace + loggerNamespaceSuffix); | ||
} | ||
var levelLog = debugLoggers[loggerNamespaceSuffix]; | ||
var color = vmDebug.useColors ? levels[level].color : ''; | ||
var reset = vmDebug.useColors ? RESET : ''; | ||
var reset = vmDebug.useColors ? exports.colorReset : ''; | ||
logger[level] = function(message, e) { | ||
logger[level] = function (message, e) { | ||
var errorStrings = getErrorMessage(e); | ||
var padding = errorStrings[1] != '' ? defaultPadding : ''; | ||
var padding = errorStrings[1] !== '' ? defaultPadding : ''; | ||
levelLog(color + levels[level].prefix + reset + message + errorStrings[0] + padding + errorStrings[1]); | ||
}; | ||
logger[level].logger = levelLog; | ||
logger[level].enabled = levelLog.enabled; | ||
}); | ||
return logger; | ||
}; | ||
} |
@@ -1,3 +0,4 @@ | ||
var log = require('../debug-logger')('myapp'); | ||
var log = require('..')('myapp'); | ||
// The below only shows up if environment variable DEBUG includes "myapp" namespace | ||
log.debug("I'm a debug output"); | ||
@@ -9,9 +10,14 @@ log.info("I'm an info output"); | ||
var debugLogger = require('../debug-logger'); | ||
if(log.isDebugEnabled){ | ||
console.log(); | ||
var debugLogger = require('..'); | ||
if (log.debug.enabled) { | ||
// This only runs if environment variable DEBUG includes "myapp:debug" namespace | ||
log.debug("Debug is enabled, let's inspect something:", debugLogger.levels); | ||
log.debug("Debug is enabled, let's inspect 'debugLogger.levels':", debugLogger.levels); | ||
} else { | ||
console.log("Debug is disabled, please add 'myapp:debug' namespace to DEBUG environment variable"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp:debug"); | ||
} | ||
console.log(); | ||
var err = new Error('error message'); | ||
@@ -22,11 +28,42 @@ err.stack = 'the stack\nline2\nline3'; | ||
log.logger('the default instance of debug, "using myapp" namespace'); | ||
log.debugLogger('the debug instance of debug, using "myapp:debug" namespace'); | ||
console.log(); | ||
log.info.logger("the default instance of debug, using 'myapp' namespace"); | ||
log.debug.logger("the debug instance of debug, using 'myapp:debug' namespace"); | ||
debugLogger.levels.info.color = '\x1b[31m'; | ||
var customColorLog = require('../debug-logger')('myapp'); | ||
customColorLog.info("I'm a RED info output"); | ||
console.log(); | ||
debugLogger.levels.error.color = debugLogger.getForeColor('magenta'); | ||
debugLogger.levels.debug.color = debugLogger.getBackColor('cyan') + debugLogger.getForeColor('white'); | ||
var customColorLog = debugLogger('myapp'); | ||
customColorLog.error("I'm a 'magenta' error output"); | ||
customColorLog.debug("I'm a 'cyan'/'white' debug output"); | ||
debugLogger.inspectOptions = { colors: true }; // Check http://nodejs.org/api/util.html#util_util_inspect_object_options | ||
log.info('nice colored example:', { anumber: 1234, astring: 'str', adate: new Date(), aboolean: true }); | ||
console.log(); | ||
debugLogger.inspectOptions = { | ||
colors : true | ||
}; | ||
// Check http://nodejs.org/api/util.html#util_util_inspect_object_options | ||
log.info('By enabling colors we get this nice colored example:', { | ||
anumber : 1234, | ||
astring : 'str', | ||
adate : new Date(), | ||
aboolean : true | ||
}); | ||
console.log(); | ||
debugLogger.levels.silly = { | ||
color : debugLogger.getForeColor('magenta'), | ||
prefix : 'SILLY ', | ||
namespaceSuffix : ':silly' | ||
}; | ||
var sillyLog = debugLogger('myapp'); | ||
sillyLog.info("Is silly logger enabled? " + sillyLog.silly.enabled); | ||
if(sillyLog.silly.enabled){ | ||
sillyLog.silly("I'm a silly output"); | ||
} else { | ||
console.log("Silly is disabled, please add 'myapp:silly' namespace to DEBUG environment variable"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp:silly"); | ||
} | ||
{ | ||
"name": "debug-logger", | ||
"version": "0.1.4", | ||
"description": "A wrapper for visionmedia's debug logger", | ||
"version": "0.2.0", | ||
"description": "A wrapper for visionmedia's debug logger, adding levels and colored output", | ||
"main": "debug-logger.js", | ||
@@ -13,2 +13,3 @@ "repository": { | ||
"logger", | ||
"levels", | ||
"colors" | ||
@@ -15,0 +16,0 @@ ], |
@@ -0,5 +1,7 @@ | ||
[![npm version](https://badge.fury.io/js/debug-logger.svg)](http://badge.fury.io/js/debug-logger) | ||
debug-logger | ||
============ | ||
A thin wrapper for visionmedia's debug logger | ||
A thin wrapper for visionmedia's debug logger, adding levels and colored output. | ||
@@ -25,7 +27,74 @@ ## Overview | ||
log.error("I'm an error output"); | ||
log.error('Something failed:', new Error('error message')); | ||
``` | ||
![screenshot](https://raw.githubusercontent.com/wiki/appscot/debug-logger/ScreenShot.png) | ||
### Inspect error/object | ||
```javascript | ||
var err = new Error('error message'); | ||
err.stack = 'the stack\nline2\nline3'; | ||
log.error('Something failed:', err); | ||
var obj = { | ||
anumber : 1234, | ||
astring : 'str', | ||
adate : new Date(), | ||
aboolean : true | ||
}; | ||
log.info("let's inspect 'obj'", obj); | ||
``` | ||
![inspect error/object](https://raw.githubusercontent.com/wiki/appscot/debug-logger/error_object.png) | ||
### Original `debug` instances and enabled property | ||
```javascript | ||
log.info.logger("the default instance of debug, using 'myapp' namespace"); | ||
log.debug.logger("the debug instance of debug, using 'myapp:debug' namespace"); | ||
if (log.debug.enabled) { | ||
// This only runs if environment variable DEBUG includes "myapp:debug" namespace | ||
log.debug("Debug is enabled"); | ||
} | ||
``` | ||
![enabled](https://raw.githubusercontent.com/wiki/appscot/debug-logger/enabled.png) | ||
### util.inspect options | ||
Full `util.inspect` options available at [nodejs.org](http://nodejs.org/api/util.html#util_util_inspect_object_options). | ||
```javascript | ||
var debugLogger = require('debug-logger'); | ||
debugLogger.inspectOptions = { | ||
colors : true | ||
}; | ||
log.info('By enabling colors we get this nice colored example:', { | ||
anumber : 1234, | ||
astring : 'str', | ||
adate : new Date(), | ||
aboolean : true | ||
}); | ||
``` | ||
![inspect](https://raw.githubusercontent.com/wiki/appscot/debug-logger/inspect.png) | ||
### Customize available log levels | ||
```javascript | ||
debugLogger.levels.error.color = debugLogger.getForeColor('magenta'); | ||
debugLogger.levels.debug.color = debugLogger.getBackColor('cyan') + debugLogger.getForeColor('white'); | ||
var customColorLog = debugLogger('myapp'); | ||
customColorLog.error("I'm a 'magenta' error output"); | ||
customColorLog.debug("I'm a 'cyan'/'white' debug output"); | ||
``` | ||
![customize log](https://raw.githubusercontent.com/wiki/appscot/debug-logger/customize_log.png) | ||
### Add log levels | ||
```javascript | ||
debugLogger.levels.silly = { | ||
color : debugLogger.getForeColor('magenta'), | ||
prefix : 'SILLY ', | ||
namespaceSuffix : ':silly' | ||
}; | ||
var sillyLog = debugLogger('myapp'); | ||
sillyLog.silly("I'm a silly output"); | ||
``` | ||
![add log levels](https://raw.githubusercontent.com/wiki/appscot/debug-logger/silly.png) | ||
More examples in the [examples folder](https://github.com/appscot/debug-logger/blob/master/examples/index.js). | ||
@@ -44,14 +113,18 @@ | ||
## Properties | ||
###### `log.logger` | ||
Returns the default debug instance. | ||
###### `getForeColor(color)` | ||
Returns an ANSI foreground color code string. `color` is one of `black, red, green, yellow, blue, magenta, cyan, white` | ||
Example: | ||
``` javascript | ||
debugLogger.getForeColor('cyan') | ||
// returns '\x1b[36m' | ||
``` | ||
###### `log.debugLogger` | ||
Returns the debug debug instance which was instanciated with "*provided_namespace*:debug". | ||
###### `getBackColor(color)` | ||
Returns an ANSI background color code string. | ||
###### `log.isEnabled` | ||
Boolean indicating if default logger is enabled. | ||
## Properties | ||
###### `log[level].logger` | ||
Returns the default debug instance used by `level`. | ||
###### `log.isDebugEnabled` | ||
Boolean indicating if debug logger is enabled. | ||
###### `log[level].enabled` | ||
Boolean indicating if `level`'s logger is enabled. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10016
145
129