debug-logger
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -7,6 +7,3 @@ 'use strict'; | ||
exports = module.exports = debugLogger; | ||
exports.getForeColor = getForeColor; | ||
exports.getBackColor = getBackColor; | ||
exports.debug = vmDebug; | ||
@@ -22,2 +19,5 @@ | ||
} | ||
if(options.levels){ | ||
merge(exports.levels, options.levels); | ||
} | ||
return debugLogger; | ||
@@ -42,4 +42,4 @@ }; | ||
trace : { | ||
color : getForeColor('cyan'), | ||
prefix : 'TRACE ', | ||
color : exports.colors.cyan, | ||
prefix : '', | ||
namespaceSuffix : ':trace', | ||
@@ -49,4 +49,4 @@ level : 0 | ||
debug : { | ||
color : getForeColor('blue'), | ||
prefix : 'DEBUG ', | ||
color : exports.colors.blue, | ||
prefix : '', | ||
namespaceSuffix : ':debug', | ||
@@ -57,18 +57,22 @@ level : 1 | ||
color : '', | ||
prefix : ' LOG ', | ||
prefix : ' ', | ||
namespaceSuffix : ':log', | ||
level : 2 | ||
}, | ||
info : { | ||
color : getForeColor('green'), | ||
prefix : ' INFO ', | ||
color : exports.colors.green, | ||
prefix : ' ', | ||
namespaceSuffix : ':info', | ||
level : 3 | ||
}, | ||
warn : { | ||
color : getForeColor('yellow'), | ||
prefix : ' WARN ', | ||
color : exports.colors.yellow, | ||
prefix : ' ', | ||
namespaceSuffix : ':warn', | ||
level : 4 | ||
}, | ||
error : { | ||
color : getForeColor('red'), | ||
prefix : ' ERROR ', | ||
color : exports.colors.red, | ||
prefix : '', | ||
namespaceSuffix : ':error', | ||
level : 5 | ||
@@ -92,2 +96,16 @@ } | ||
function merge(object, source){ | ||
Object.keys(source).forEach(function(key){ | ||
var val = source[key]; | ||
if(!object[key] || !isObject(val)){ | ||
object[key] = val; | ||
return; | ||
} | ||
Object.keys(val).forEach(function(idx){ | ||
object[key][idx] = val[idx]; | ||
}); | ||
}); | ||
} | ||
function getLogLevel(namespace) { | ||
@@ -140,6 +158,10 @@ if(!process.env.DEBUG_LEVEL) { | ||
function isObject(obj){ | ||
return typeof obj === 'object' || obj instanceof Object; | ||
} | ||
function hasFormattingElements(str){ | ||
if(!str) { return false; } | ||
var res = false; | ||
['%s', '%d', '%j'].forEach(function(elem){ | ||
['%s', '%d', '%j', '%o'].forEach(function(elem){ | ||
if(str.indexOf(elem) >= 0) { | ||
@@ -153,3 +175,3 @@ res = true; | ||
function getErrorMessage(e) { | ||
var errorStrings = [' ' + e]; | ||
var errorStrings = ['' + e]; | ||
@@ -166,3 +188,3 @@ if (typeof e === 'undefined') { | ||
if (e instanceof Error) { | ||
errorStrings[0] = ' ' + e.toString(); | ||
errorStrings[0] = e.toString(); | ||
if (e.stack) { | ||
@@ -174,10 +196,10 @@ errorStrings[1] = 'Stack trace'; | ||
} | ||
if (typeof e === 'object' || e instanceof Object) { | ||
if (isObject(e)) { | ||
var inspection = util.inspect(e, exports.inspectOptions); | ||
if(inspection.length < 55){ | ||
errorStrings[0] = ' ' + inspection; | ||
errorStrings[0] = inspection; | ||
return errorStrings; | ||
} | ||
if (typeof e.toString !== 'undefined') { | ||
errorStrings[0] = ' ' + e.toString(); | ||
errorStrings[0] = e.toString(); | ||
} | ||
@@ -192,13 +214,18 @@ errorStrings[1] = 'Inspected object'; | ||
function getForeColor(color){ | ||
return '\x1b[' + (30 + exports.colors[color]) + 'm'; | ||
if(!isNaN(color)){ | ||
return '\x1b[3' + color + 'm'; | ||
} | ||
else if(exports.colors[color]){ | ||
return '\x1b[3' + exports.colors[color] + 'm'; | ||
} | ||
return color; | ||
} | ||
function getBackColor(color){ | ||
return '\x1b[' + (40 + exports.colors[color]) + 'm'; | ||
} | ||
var debugInstances = {}; | ||
function getDebugInstance(namespace){ | ||
function getDebugInstance(namespace, color){ | ||
if(!debugInstances[namespace]){ | ||
debugInstances[namespace] = vmDebug(namespace); | ||
if(!isNaN(color)){ | ||
debugInstances[namespace].color = color; | ||
} | ||
} | ||
@@ -219,6 +246,6 @@ return debugInstances[namespace]; | ||
if(!debugLoggers[loggerNamespaceSuffix]){ | ||
debugLoggers[loggerNamespaceSuffix] = getDebugInstance.bind(this, namespace + loggerNamespaceSuffix); | ||
debugLoggers[loggerNamespaceSuffix] = getDebugInstance.bind(this, namespace + loggerNamespaceSuffix, levels[levelName].color); | ||
} | ||
var levelLogger = debugLoggers[loggerNamespaceSuffix]; | ||
var color = vmDebug.useColors ? levels[levelName].color : ''; | ||
var color = vmDebug.useColors ? getForeColor(levels[levelName].color) : ''; | ||
var reset = vmDebug.useColors ? exports.colorReset : ''; | ||
@@ -249,3 +276,3 @@ var inspectionHighlight = vmDebug.useColors ? exports.styles.underline : ''; | ||
param = errorStrings[i]; | ||
message += param[0]; | ||
message += i === 0 ? param[0] : ' ' + param[0]; | ||
if (param.length > 1) { | ||
@@ -252,0 +279,0 @@ var highlightStack = param[1].indexOf('Stack') >= 0 ? color : ''; |
var log = require('..')('myapp'); | ||
// The below only shows up if environment variable DEBUG includes "myapp" namespace | ||
// The below only shows up if environment variable DEBUG includes "myapp:*" namespace | ||
log.trace("I'm a trace output"); | ||
@@ -15,3 +15,3 @@ log.debug("I'm a debug output"); | ||
if (log.debug.enabled()) { | ||
// This only runs if environment variable DEBUG includes "myapp:debug" namespace | ||
// This only runs if environment variable DEBUG includes "myapp:debug" or "myapp:*" namespace | ||
log.debug("Debug is enabled, let's inspect 'debugLogger.levels':", debugLogger.levels); | ||
@@ -41,3 +41,2 @@ } else { | ||
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"); | ||
@@ -49,7 +48,6 @@ var debug = debugLogger.debug('myapp:visionmedia'); | ||
console.log(); | ||
debugLogger.levels.error.color = debugLogger.getForeColor('magenta'); | ||
debugLogger.levels.debug.color = debugLogger.getBackColor('cyan') + debugLogger.getForeColor('white'); | ||
debugLogger.levels.error.color = debugLogger.colors.magenta; | ||
debugLogger.levels.error.prefix = 'ERROR '; | ||
var customColorLog = debugLogger('myapp'); | ||
customColorLog.error("I'm a 'magenta' error output"); | ||
customColorLog.debug("I'm a 'cyan'/'white' debug output"); | ||
@@ -72,4 +70,4 @@ | ||
debugLogger.levels.silly = { | ||
color : debugLogger.getForeColor('magenta'), | ||
prefix : 'SILLY ', | ||
color : debugLogger.colors.magenta, | ||
prefix : 'SILLY ', | ||
namespaceSuffix : ':silly', | ||
@@ -94,6 +92,6 @@ level : 0 | ||
if (!log.error.enabled()) { | ||
// This only runs if environment variable DEBUG includes "myapp" namespace | ||
console.log("You probably haven't seen much because the default logger is disabled"); | ||
console.log("Please add 'myapp' namespace to DEBUG environment variable and try again"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp"); | ||
// This only runs if environment variable DEBUG includes "myapp:*" namespace | ||
console.log("\nYou probably haven't seen much because some loggers are disabled"); | ||
console.log("Please add 'myapp:*' namespace to DEBUG environment variable and try again"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp:*"); | ||
} else if(log.log.enabled()) { | ||
@@ -100,0 +98,0 @@ console.log("\nNow set DEBUG_LEVEL environment variable to warn and run this example again"); |
{ | ||
"name": "debug-logger", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A wrapper for visionmedia/debug logger, adding levels and colored output", | ||
@@ -5,0 +5,0 @@ "main": "debug-logger.js", |
@@ -10,3 +10,3 @@ [![npm version](https://badge.fury.io/js/debug-logger.svg)](http://badge.fury.io/js/debug-logger) | ||
[visionmedia/debug](https://github.com/visionmedia/debug) is a ubitiquous logging library with 1000+ dependants. Given how widespread it is and the convenience of namespaces it is a great logger for library modules. | ||
`debug-logger` is a convenience wrapper around `debug` that adds level based coloured output. Each instance of `debug-logger` will lazily instantiate 3 instances of `debug`, one for general purpose logging using `namespace`, another for debug (`namespace:debug`) and another for trace (`namespace:trace`). All this in configurable fashion. `debug-logger` has no dependencies besides `debug`. | ||
`debug-logger` is a convenience wrapper around `debug` that adds level based coloured output. Each instance of `debug-logger` will lazily instantiate several instances of `debug` such as `namespace:info`, `namespace:warn`, `namespace:error`, etc. All these are configurable. `debug-logger` has no dependencies besides `debug`. | ||
@@ -24,3 +24,5 @@ At AppsCot we use `debug-logger` in [waterline-orientdb](https://github.com/appscot/waterline-orientdb). | ||
log.trace("I'm a trace output"); | ||
log.debug("I'm a debug output"); | ||
log.log("I'm a log output"); | ||
log.info("I'm an info output"); | ||
@@ -51,4 +53,5 @@ log.warn("I'm a warn output"); | ||
```javascript | ||
log.info.logger()("the default instance of debug, using 'myapp' namespace"); | ||
log.debug.logger()("the debug instance of debug, using 'myapp:debug' namespace"); | ||
var debug = debugLogger.debug('myapp:visionmedia'); | ||
debug('Nothing tastes better than the original!'); | ||
@@ -80,8 +83,7 @@ if (log.debug.enabled()) { | ||
```javascript | ||
debugLogger.levels.error.color = debugLogger.getForeColor('magenta'); | ||
debugLogger.levels.debug.color = debugLogger.getBackColor('cyan') + debugLogger.getForeColor('white'); | ||
debugLogger.levels.error.color = debugLogger.colors.magenta; | ||
debugLogger.levels.error.prefix = 'ERROR '; | ||
var customColorLog = debugLogger('myapp'); | ||
customColorLog.error("I'm a 'magenta' error output"); | ||
customColorLog.debug("I'm a 'cyan'/'white' debug output"); | ||
``` | ||
@@ -93,3 +95,3 @@ ![customize log](https://raw.githubusercontent.com/wiki/appscot/debug-logger/customize_log.png) | ||
debugLogger.levels.silly = { | ||
color : debugLogger.getForeColor('magenta'), | ||
color : debugLogger.colors.magenta, | ||
prefix : 'SILLY ', | ||
@@ -104,3 +106,10 @@ namespaceSuffix : ':silly' | ||
### Filter log level (instead of namespace) | ||
### Multiple arguments / util.format style | ||
```javascript | ||
log.log("Multiple", "arguments", "including", "objects:", { obj: 'obj'}, "makes life easier"); | ||
log.warn("util.format style string: %s, number: %d and json: %j.", "foo", 13, { obj: 'json'}); | ||
``` | ||
![multiple arguments](https://raw.githubusercontent.com/wiki/appscot/debug-logger/arguments.png) | ||
### Filter by log level (instead of namespace) | ||
```sh | ||
@@ -111,3 +120,3 @@ export DEBUG_LEVEL=info | ||
More examples in the [examples folder](https://github.com/appscot/debug-logger/blob/master/examples/index.js). | ||
More examples in the [examples folder](https://github.com/appscot/debug-logger/blob/master/examples). | ||
@@ -126,6 +135,6 @@ ## Reference | ||
#### `log.error([data][, ...])` | ||
Prints the data prepended by log level. If the terminal supports colors, the level will be one of: blue, green, yellow, red. If an Error is provided, the toString() and call stack will be outputted. If an Object is provided the toString() and util.inspect() will be outputted. Example: | ||
Prints the data prepended by log level. If the terminal supports colors, each level will have a specific color. If an Error is provided, the toString() and call stack will be outputted. If an Object is provided the toString() and util.inspect() will be outputted. Example: | ||
``` | ||
myapp:debug DEBUG I'm a debug output +0ms | ||
myapp INFO I'm an info output +1ms | ||
myapp:debug I'm a debug output +0ms | ||
myapp:info I'm an info output +1ms | ||
``` | ||
@@ -142,15 +151,6 @@ This function can take multiple arguments in a printf()-like way, if formatting elements are not found in the first string then util.inspect is used on each argument. | ||
#### `.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' | ||
``` | ||
#### `.config(obj)` | ||
Configures debug-logger. Returns `debug-logger` to allow chaining operations. | ||
#### `.getBackColor(color)` | ||
Returns an ANSI background color code string. | ||
#### `.debug` | ||
Returns visionmedia/debug module. |
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
18315
8
414