debug-logtron
Advanced tools
Comparing version 3.2.0 to 4.0.0
@@ -36,15 +36,23 @@ 'use strict'; | ||
self.whitelists = { | ||
fatal: {}, | ||
error: {}, | ||
warn: {}, | ||
access: {}, | ||
info: {}, | ||
debug: {}, | ||
trace: {} | ||
}; | ||
self.records = []; | ||
var debugEnviron = self.env.NODE_DEBUG || ''; | ||
var regex = new RegExp('\\b' + self.namespace + '\\b', 'i'); | ||
var verboseRegex = new RegExp( | ||
'\\b' + self.namespace + 'verbose\\b', 'i' | ||
); | ||
self.enabled = regex.test(debugEnviron); | ||
self.verbose = verboseRegex.test(debugEnviron); | ||
self.enabled = typeof opts.enabled === 'boolean' ? | ||
opts.enabled : true; | ||
self.verbose = opts.verbose || regex.test(debugEnviron); | ||
self.trace = typeof opts.trace === 'boolean' ? | ||
opts.trace : (self.verbose && !!self.env.TRACE); | ||
if (opts.verbose) { | ||
self.verbose = true; | ||
} | ||
if (self.verbose || opts.enabled) { | ||
if (self.verbose) { | ||
self.enabled = true; | ||
@@ -54,17 +62,17 @@ } | ||
DebugLogBackend.prototype.whitelist = function whitelist(level, msg) { | ||
var self = this; | ||
self.whitelists[level][msg] = true; | ||
}; | ||
DebugLogBackend.prototype.createStream = function createStream() { | ||
var self = this; | ||
return DebugLogStream(self.namespace, { | ||
console: self.console, | ||
assert: self.assert, | ||
colors: self.colors, | ||
enabled: self.enabled, | ||
verbose: self.verbose | ||
}); | ||
return DebugLogStream(self.namespace, self); | ||
}; | ||
function DebugLogStream(namespace, opts) { | ||
function DebugLogStream(namespace, backend) { | ||
if (!(this instanceof DebugLogStream)) { | ||
return new DebugLogStream(namespace, opts); | ||
return new DebugLogStream(namespace, backend); | ||
} | ||
@@ -75,7 +83,3 @@ | ||
self.namespace = namespace; | ||
self.console = opts.console; | ||
self.assert = opts.assert; | ||
self.colors = opts.colors; | ||
self.enabled = opts.enabled; | ||
self.verbose = opts.verbose; | ||
self.backend = backend; | ||
} | ||
@@ -89,15 +93,25 @@ | ||
var whitelist = self.backend.whitelists[levelName]; | ||
if (whitelist[logRecord.fields.msg]) { | ||
self.backend.records.push(logRecord); | ||
if (cb) { | ||
cb(); | ||
} | ||
return; | ||
} | ||
if ( | ||
(levelName === 'fatal' || levelName === 'error') || | ||
(self.enabled && | ||
(self.backend.enabled && | ||
(levelName === 'warn' || levelName === 'info')) || | ||
(self.verbose && | ||
(levelName === 'access' || levelName === 'debug' || | ||
levelName === 'trace')) | ||
(self.backend.verbose && | ||
(levelName === 'access' || levelName === 'debug')) || | ||
(self.backend.trace && levelName === 'trace') | ||
) { | ||
var msg = self.formatMessage(logRecord); | ||
if (self.assert) { | ||
self.assert.comment(msg); | ||
if (self.backend.assert) { | ||
self.backend.assert.comment(msg); | ||
} else { | ||
self.console.error(msg); | ||
self.backend.console.error(msg); | ||
} | ||
@@ -123,3 +137,3 @@ } | ||
if (self.colors) { | ||
if (self.backend.colors) { | ||
prefix = chalk[color](prefix); | ||
@@ -126,0 +140,0 @@ prefix = chalk.bold(prefix); |
@@ -60,2 +60,10 @@ 'use strict'; | ||
proto.whitelist = function whitelist(level, msg) { | ||
this._backend.whitelist(level, msg); | ||
}; | ||
proto.items = function items() { | ||
return this._backend.records; | ||
}; | ||
proto.trace = function trace(msg, meta, cb) { | ||
@@ -62,0 +70,0 @@ this._log(LEVELS.trace, msg, meta, cb); |
@@ -18,3 +18,3 @@ 'use strict'; | ||
if (!(this instanceof LogMessage)) { | ||
return new LogMessage(level, msg, meta); | ||
return new LogMessage(level, msg, meta, time); | ||
} | ||
@@ -21,0 +21,0 @@ |
{ | ||
"name": "debug-logtron", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "A debug logger with a logtron interface.", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -15,4 +15,4 @@ # debug-logtron | ||
This logger does nothing unless you start the process with | ||
`NODE_DEBUG=mylibrary`. | ||
This logger is designed for tests; it prints info & above | ||
and prints debugs if you set `NODE_DEBUG=mylibrary` | ||
@@ -30,9 +30,33 @@ ```js | ||
`debug-logtron` will internally use [`debuglog`][debuglog] to | ||
write your logs. | ||
It writes all logs to stderr. If you call `logger.error()` or | ||
`logger.fatal()` it will throw exceptions. any error callsites | ||
are bugs. | ||
This means its silent by default unless you start the process | ||
with the `NODE_DEBUG` environment variable. If you set | ||
`NODE_DEBUG=mylibrary` it will write to stderr. | ||
warns go to stderr by default. | ||
## Using in tests | ||
You can use the `.whitelist()` and `.items()` method to make | ||
testing easier | ||
``` | ||
var DebugLogtron = require('debug-logtron'); | ||
var test = require('tape'); | ||
test('some module', function t(assert) { | ||
var logger = NullLogtron('mything'); | ||
var thing = new Thing({ logger: logger }) | ||
logger.whitelist('error', 'some msg'); | ||
thing.doStuff(); | ||
var items = logger.items(); | ||
assert.equal(items.filter(function (x) { | ||
return x.levelName === 'error' | ||
}).length, 1, 'thing writes to logger.error()'); | ||
assert.end(); | ||
}); | ||
``` | ||
## Interface | ||
@@ -42,9 +66,11 @@ | ||
Any warns and infos can be made visable using `NODE_DEBUG=mylibrary`. | ||
Any warns and infos got to stderr. | ||
Any debugs / access / trace can be made visible using | ||
`NODE_DEBUG=mylibraryverbose`. | ||
Any debugs / access can be made visible using | ||
`NODE_DEBUG=mylibrary`. | ||
You can turn colors off with `--color false` | ||
If you want to see trace() logs you must set `NODE_DEBUG=mylibrary TRACE=1` | ||
## Alternatives | ||
@@ -51,0 +77,0 @@ |
@@ -224,3 +224,4 @@ 'use strict'; | ||
} | ||
} | ||
}, | ||
enabled: false | ||
}); | ||
@@ -242,3 +243,3 @@ | ||
test('prints warn/info if NODE_DEBUG', function t(assert) { | ||
test('prints warn/info by default', function t(assert) { | ||
var lines = []; | ||
@@ -250,5 +251,2 @@ var logger = DebugLogtron('wat', { | ||
} | ||
}, | ||
env: { | ||
NODE_DEBUG: 'wat' | ||
} | ||
@@ -294,3 +292,3 @@ }); | ||
test('prints debug/access/trace if NODE_DEBUG verbose', function t(assert) { | ||
test('does not prints warn/info if disabled', function t(assert) { | ||
var lines = []; | ||
@@ -303,4 +301,26 @@ var logger = DebugLogtron('wat', { | ||
}, | ||
enabled: false | ||
}); | ||
logger.info('hi'); | ||
assert.equal(lines.length, 0); | ||
lines = []; | ||
logger.debug('roflcopter'); | ||
assert.equal(lines.length, 0); | ||
assert.end(); | ||
}); | ||
test('prints debug/access/trace if NODE_DEBUG', function t(assert) { | ||
var lines = []; | ||
var logger = DebugLogtron('wat', { | ||
console: { | ||
error: function log(x) { | ||
lines.push(x); | ||
} | ||
}, | ||
env: { | ||
NODE_DEBUG: 'watverbose' | ||
NODE_DEBUG: 'wat' | ||
} | ||
@@ -387,2 +407,27 @@ }); | ||
test('can whitelist errors', function t(assert) { | ||
var logger = allocLogger(); | ||
assert.throws(function throwIt() { | ||
logger.error('oh hi'); | ||
}, /oh hi/); | ||
logger.whitelist('error', 'oh hi'); | ||
logger.error('oh hi'); | ||
assert.equal(logger.items().length, 1); | ||
assert.equal(logger.items()[0].fields.msg, 'oh hi'); | ||
assert.throws(function throwIt() { | ||
logger.error('oh hi 2'); | ||
}, /oh hi 2/); | ||
logger.error('oh hi', {}, function onMsg() { | ||
assert.equal(logger.items().length, 2); | ||
assert.end(); | ||
}); | ||
}); | ||
function allocLogger(opts) { | ||
@@ -392,4 +437,3 @@ opts = opts || {}; | ||
env: { | ||
NODE_DEBUG: 'debuglogtrontestcode ' + | ||
'debuglogtrontestcodeverbose' | ||
NODE_DEBUG: 'debuglogtrontestcode' | ||
}, | ||
@@ -403,2 +447,3 @@ console: { | ||
}, | ||
trace: true, | ||
colors: opts.colors | ||
@@ -405,0 +450,0 @@ }); |
26378
610
125