debug-logger
Advanced tools
Comparing version 0.3.0 to 0.3.1
'use strict'; | ||
var util = require('util'); | ||
var vmDebug = require('debug'); | ||
var util = require('util'), | ||
vmDebug = require('debug'), | ||
streamSpy = require('./stream-spy'); | ||
exports = module.exports = debugLogger; | ||
@@ -10,2 +13,13 @@ exports.getForeColor = getForeColor; | ||
exports.config = function config(options){ | ||
options = options || {}; | ||
if(options.ensureNewline){ | ||
ensureNewline(); | ||
} | ||
if(options.inspectOptions){ | ||
exports.inspectOptions = options.inspectOptions; | ||
} | ||
return debugLogger; | ||
}; | ||
exports.inspectOptions = {}; | ||
@@ -61,7 +75,15 @@ | ||
exports.styles = { | ||
bold : '\x1b[1m', | ||
underline : '\x1b[4m', | ||
inverse : '\x1b[7m' | ||
underline : '\x1b[4m' | ||
}; | ||
var ensureNewlineEnabled = false; | ||
var fd = parseInt(process.env.DEBUG_FD, 10) || 2; | ||
function ensureNewline(){ | ||
if(fd !== 1 && fd !== 2){ return; } | ||
streamSpy.enable(); | ||
ensureNewlineEnabled = true; | ||
return debugLogger; | ||
} | ||
function getLogLevel(namespace) { | ||
@@ -146,2 +168,7 @@ if(!process.env.DEBUG_LEVEL) { | ||
if (typeof e === 'object' || e instanceof Object) { | ||
var inspection = util.inspect(e, exports.inspectOptions); | ||
if(inspection.length < 55){ | ||
errorStrings[0] = ' ' + inspection; | ||
return errorStrings; | ||
} | ||
if (typeof e.toString !== 'undefined') { | ||
@@ -151,3 +178,3 @@ errorStrings[0] = ' ' + e.toString(); | ||
errorStrings[1] = 'Inspected object'; | ||
errorStrings[2] = util.inspect(e, exports.inspectOptions); | ||
errorStrings[2] = inspection; | ||
} | ||
@@ -158,6 +185,2 @@ | ||
function getPadding(size){ | ||
return new Array(size+1).join(' '); | ||
} | ||
function getForeColor(color){ | ||
@@ -171,3 +194,2 @@ return '\x1b[' + (30 + exports.colors[color]) + 'm'; | ||
var debugInstances = {}; | ||
@@ -181,5 +203,5 @@ function getDebugInstance(namespace){ | ||
function debugLogger(namespace) { | ||
var levels = exports.levels; | ||
var defaultPadding = '\n'; | ||
var debugLoggers = { 'default': getDebugInstance.bind(this, namespace) }; | ||
@@ -198,8 +220,10 @@ | ||
var reset = vmDebug.useColors ? exports.colorReset : ''; | ||
var inspectionHighlight = vmDebug.useColors ? exports.styles.bold : ''; | ||
var inspectionHighlight = vmDebug.useColors ? exports.styles.underline : ''; | ||
logger[levelName] = function () { | ||
function logFn() { | ||
if (logger.logLevel > logger[levelName].level) { return; } | ||
var levelLog = levelLogger(); | ||
if(!levelLog.enabled) { return; } | ||
if (isString(arguments[0]) && hasFormattingElements(arguments[0])){ | ||
@@ -224,4 +248,4 @@ arguments[0] = color + levels[levelName].prefix + reset + arguments[0]; | ||
var highlightStack = param[1].indexOf('Stack') >= 0 ? color : ''; | ||
inspections += defaultPadding + | ||
inspectionHighlight + '\\/\\/ ' + param[1] + ' #' + n++ + ' \\/\\/' + reset + '\n' + | ||
inspections += '\n' + | ||
inspectionHighlight + '___' + param[1] + ' #' + n++ + '___' + reset +'\n' + | ||
highlightStack + param[2] + reset; | ||
@@ -233,6 +257,14 @@ } | ||
}; | ||
function logNewlineFn() { | ||
if (streamSpy.lastCharacter !== '\n') { | ||
vmDebug.log(''); | ||
} | ||
logFn.apply(logFn, arguments); | ||
}; | ||
logger[levelName] = ensureNewlineEnabled ? logNewlineFn : logFn; | ||
logger[levelName].level = levels[levelName].level; | ||
logger[levelName].logger = function(){ return levelLogger(); }; | ||
logger[levelName].enabled = function(){ return levelLogger().enabled; }; | ||
logger[levelName].enabled = function(){ return logger.logLevel <= logger[levelName].level && levelLogger().enabled; }; | ||
}); | ||
@@ -239,0 +271,0 @@ |
@@ -80,6 +80,12 @@ var log = require('..')('myapp'); | ||
console.log("Silly is disabled, please add 'myapp:silly' namespace to DEBUG environment variable"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp:silly\n"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp:silly"); | ||
} | ||
if (!log.log.enabled()) { | ||
console.log(); | ||
var alwaysPrintAtStartOfLineLog = debugLogger.config({ ensureNewline: true })('myapp'); | ||
process.stdout.write('Some text without a line break'); | ||
alwaysPrintAtStartOfLineLog.warn('from the start'); | ||
if (!log.error.enabled()) { | ||
// This only runs if environment variable DEBUG includes "myapp" namespace | ||
@@ -89,3 +95,3 @@ console.log("You probably haven't seen much because the default logger is disabled"); | ||
console.log("e.g.: export DEBUG=$DEBUG,myapp"); | ||
} else { | ||
} else if(log.log.enabled()) { | ||
console.log("\nNow set DEBUG_LEVEL environment variable to warn and run this example again"); | ||
@@ -92,0 +98,0 @@ console.log("e.g.: export DEBUG_LEVEL=warn"); |
{ | ||
"name": "debug-logger", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A wrapper for visionmedia/debug logger, adding levels and colored output", | ||
@@ -5,0 +5,0 @@ "main": "debug-logger.js", |
@@ -10,5 +10,5 @@ [![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` contains 2 instances of `debug`, one for general purpose logging and another using `namespace:debug` for debug logs. | ||
`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`. | ||
AppsCot uses `debug-logger` in [waterline-orientdb](https://github.com/appscot/waterline-orientdb). | ||
At AppsCot we use `debug-logger` in [waterline-orientdb](https://github.com/appscot/waterline-orientdb). | ||
@@ -50,6 +50,6 @@ ## Instalation | ||
```javascript | ||
log.info.logger("the default instance of debug, using 'myapp' namespace"); | ||
log.debug.logger("the debug instance of debug, using 'myapp:debug' namespace"); | ||
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) { | ||
if (log.debug.enabled()) { | ||
// This only runs if environment variable DEBUG includes "myapp:debug" namespace | ||
@@ -101,8 +101,16 @@ log.debug("Debug is enabled"); | ||
### Filter log level (instead of namespace) | ||
```sh | ||
export DEBUG_LEVEL=info | ||
``` | ||
Only info level and above logs will be outputted. | ||
More examples in the [examples folder](https://github.com/appscot/debug-logger/blob/master/examples/index.js). | ||
## Methods | ||
## Reference | ||
### Instance Methods | ||
Assuming log is an instance of debug-logger (`var log = require('debug-logger')('myapp');`). | ||
#### `log.trace([data][, ...])` | ||
@@ -127,5 +135,5 @@ #### `log.debug([data][, ...])` | ||
### Module Methods | ||
### Module | ||
#### `getForeColor(color)` | ||
#### `.getForeColor(color)` | ||
Returns an ANSI foreground color code string. `color` is one of `black, red, green, yellow, blue, magenta, cyan, white` | ||
@@ -138,7 +146,7 @@ Example: | ||
#### `getBackColor(color)` | ||
#### `.getBackColor(color)` | ||
Returns an ANSI background color code string. | ||
#### `debug` | ||
#### `.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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16869
7
348
148
0
3