debug-logtron
Advanced tools
Comparing version 2.1.1 to 3.0.0
'use strict'; | ||
var globalDebuglog = require('debuglog'); | ||
var inspect = require('util').inspect; | ||
var process = require('process'); | ||
var globalConsole = require('console'); | ||
var chalk = require('chalk'); | ||
var COLOR_MAP = { | ||
fatal: 'bgRed', | ||
error: 'bgRed', | ||
warn: 'bgYellow', | ||
access: 'bgGreen', | ||
info: 'bgGreen', | ||
debug: 'bgBlue', | ||
trace: 'bgCyan' | ||
}; | ||
module.exports = DebugLogBackend; | ||
@@ -13,30 +25,92 @@ | ||
var debuglog = opts.debuglog || | ||
/*istanbul ignore next */ globalDebuglog; | ||
var self = this; | ||
this.log = debuglog(namespace); | ||
self.console = opts.console || globalConsole; | ||
self.colors = typeof opts.colors === 'boolean' ? | ||
opts.colors : true; | ||
/*eslint no-process-env: 0*/ | ||
self.env = opts.env || process.env; | ||
self.namespace = namespace.toUpperCase(); | ||
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); | ||
if (self.verbose) { | ||
self.enabled = true; | ||
} | ||
} | ||
var proto = DebugLogBackend.prototype; | ||
DebugLogBackend.prototype.createStream = function createStream() { | ||
var self = this; | ||
proto.createStream = function createStream() { | ||
return DebugLogStream(self.namespace, { | ||
console: self.console, | ||
colors: self.colors, | ||
enabled: self.enabled, | ||
verbose: self.verbose | ||
}); | ||
}; | ||
function DebugLogStream(namespace, opts) { | ||
if (!(this instanceof DebugLogStream)) { | ||
return new DebugLogStream(namespace, opts); | ||
} | ||
var self = this; | ||
var stream = { | ||
write: write | ||
}; | ||
self.namespace = namespace; | ||
self.console = opts.console; | ||
self.colors = opts.colors; | ||
self.enabled = opts.enabled; | ||
self.verbose = opts.verbose; | ||
} | ||
return stream; | ||
DebugLogStream.prototype.write = function write(logRecord, cb) { | ||
var self = this; | ||
function write(logRecord, cb) { | ||
var msg = logRecord.levelName + ': ' + | ||
logRecord.fields.msg + ' ~ ' + | ||
inspect(logRecord.meta); | ||
var levelName = logRecord.levelName; | ||
self.log(msg); | ||
if ( | ||
(levelName === 'fatal' || levelName === 'error') || | ||
(self.enabled && | ||
(levelName === 'warn' || levelName === 'info')) || | ||
(self.verbose && | ||
(levelName === 'access' || levelName === 'debug' || | ||
levelName === 'trace')) | ||
) { | ||
var msg = self.formatMessage(logRecord); | ||
self.console.error(msg); | ||
} | ||
if (cb) { | ||
cb(); | ||
} | ||
if (levelName === 'fatal' || levelName === 'error') { | ||
throw new Error(logRecord.fields.msg); | ||
} | ||
if (cb) { | ||
cb(); | ||
} | ||
}; | ||
DebugLogStream.prototype.formatMessage = | ||
function formatMessage(logRecord) { | ||
var self = this; | ||
var pid = process.pid; | ||
var prefix = self.namespace + ' ' + pid + ': ' + | ||
logRecord.levelName.toUpperCase(); | ||
var color = COLOR_MAP[logRecord.levelName]; | ||
if (self.colors) { | ||
prefix = chalk[color](prefix); | ||
prefix = chalk.bold(prefix); | ||
} | ||
return prefix + ': ' + logRecord.fields.msg + ' ~ ' + | ||
inspect(logRecord.meta); | ||
}; |
{ | ||
"name": "debug-logtron", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "A debug logger with a logtron interface.", | ||
@@ -20,2 +20,3 @@ "keywords": [], | ||
"dependencies": { | ||
"chalk": "1.0.0", | ||
"debuglog": "^1.0.1", | ||
@@ -22,0 +23,0 @@ "error": "^5.0.0", |
@@ -36,2 +36,13 @@ # debug-logtron | ||
## Interface | ||
This library will re `throw` any `.error()` or `.fatal()` callsites. | ||
Any warns and infos can be made visable using `NODE_DEBUG=mylibrary`. | ||
Any debugs / access / trace can be made visible using | ||
`NODE_DEBUG=mylibraryverbose`. | ||
You can turn colors off with `--color false` | ||
## Alternatives | ||
@@ -43,2 +54,3 @@ | ||
then I strongly recommend you use [`null-logtron`][null-logtron] | ||
## Motivation | ||
@@ -45,0 +57,0 @@ |
@@ -6,3 +6,6 @@ 'use strict'; | ||
var os = require('os'); | ||
var chalk = require('chalk'); | ||
chalk.enabled = false; | ||
var DebugLogtron = require('../index.js'); | ||
@@ -25,4 +28,3 @@ var LogMessage = require('../log-message.js'); | ||
var line = logger.lines[0]; | ||
assert.equal(line.namespace, 'debuglogtron'); | ||
assert.equal(line.msg, 'debug: hi ~ null'); | ||
assert.ok(line.msg.indexOf('DEBUG: hi ~ null') >= 0); | ||
@@ -42,4 +44,3 @@ assert.end(); | ||
var line = logger.lines[0]; | ||
assert.equal(line.namespace, 'debuglogtron'); | ||
assert.equal(line.msg, 'debug: oh hi ~ {}'); | ||
assert.ok(line.msg.indexOf('DEBUG: oh hi ~ {}') >= 0); | ||
@@ -81,34 +82,32 @@ assert.end(); | ||
logger.warn('warn'); | ||
logger.error('error'); | ||
logger.fatal('fatal'); | ||
assert.equal(logger.lines.length, 7); | ||
assert.equal(logger.lines.length, 5); | ||
var line = logger.lines[0]; | ||
assert.equal(line.namespace, 'debuglogtron'); | ||
assert.equal(line.msg, 'trace: trace ~ null'); | ||
assert.ok(line.msg.indexOf('TRACE: trace ~ null') >= 0); | ||
var line2 = logger.lines[1]; | ||
assert.equal(line2.namespace, 'debuglogtron'); | ||
assert.equal(line2.msg, 'debug: debug ~ null'); | ||
assert.ok(line2.msg.indexOf('DEBUG: debug ~ null') >= 0); | ||
var line3 = logger.lines[2]; | ||
assert.equal(line3.namespace, 'debuglogtron'); | ||
assert.equal(line3.msg, 'info: info ~ null'); | ||
assert.ok(line3.msg.indexOf('INFO: info ~ null') >= 0); | ||
var line4 = logger.lines[3]; | ||
assert.equal(line4.namespace, 'debuglogtron'); | ||
assert.equal(line4.msg, 'access: access ~ null'); | ||
assert.ok(line4.msg.indexOf('ACCESS: access ~ null') >= 0); | ||
var line5 = logger.lines[4]; | ||
assert.equal(line5.namespace, 'debuglogtron'); | ||
assert.equal(line5.msg, 'warn: warn ~ null'); | ||
assert.ok(line5.msg.indexOf('WARN: warn ~ null') >= 0); | ||
assert.throws(function throwIt() { | ||
logger.error('error'); | ||
}, 'error'); | ||
assert.throws(function throwIt() { | ||
logger.fatal('fatal'); | ||
}, 'fatal'); | ||
var line6 = logger.lines[5]; | ||
assert.equal(line6.namespace, 'debuglogtron'); | ||
assert.equal(line6.msg, 'error: error ~ null'); | ||
assert.ok(line6.msg.indexOf('ERROR: error ~ null') >= 0); | ||
var line7 = logger.lines[6]; | ||
assert.equal(line7.namespace, 'debuglogtron'); | ||
assert.equal(line7.msg, 'fatal: fatal ~ null'); | ||
assert.ok(line7.msg.indexOf('FATAL: fatal ~ null') >= 0); | ||
@@ -140,5 +139,4 @@ assert.end(); | ||
assert.equal(line.namespace, 'debuglogtron'); | ||
assert.equal(line.msg, 'info: hello ~ ' + | ||
'{ complex: { nested: true, foo: \'bar\' } }'); | ||
assert.ok(line.msg.indexOf('INFO: hello ~ ' + | ||
'{ complex: { nested: true, foo: \'bar\' } }') >= 0); | ||
@@ -200,20 +198,128 @@ assert.end(); | ||
function allocLogger() { | ||
/* eslint no-process-env: 0 */ | ||
var prev = process.env.NODE_DEBUG; | ||
process.env.NODE_DEBUG = 'debuglogtrontestcode'; | ||
var logger = DebugLogtron('debuglogtron', { | ||
debuglog: function fakeDebuglog(namespace) { | ||
return function logStatement(msg) { | ||
test('logger respects color option', function t(assert) { | ||
chalk.enabled = true; | ||
var logger1 = allocLogger({ | ||
colors: false | ||
}); | ||
var logger2 = allocLogger({ | ||
colors: true | ||
}); | ||
logger1.info('hi'); | ||
logger2.info('hi'); | ||
var line1 = logger1.lines[0].msg; | ||
assert.ok(line1.indexOf('INFO: hi ~ null') >= 0); | ||
var line2 = logger2.lines[0].msg; | ||
assert.ok( | ||
line2.indexOf('INFO\u001b[49m\u001b[22m: hi ~ null') >= 0 | ||
); | ||
chalk.enabled = false; | ||
assert.end(); | ||
}); | ||
test('always prints error/fatal', function t(assert) { | ||
var lines = []; | ||
var logger = DebugLogtron('wat', { | ||
console: { | ||
error: function log(x) { | ||
lines.push(x); | ||
} | ||
} | ||
}); | ||
assert.throws(function throwIt() { | ||
logger.error('hi'); | ||
}, 'hi'); | ||
assert.equal(lines.length, 1); | ||
var line = lines[0]; | ||
assert.ok(line.indexOf('ERROR: hi ~ null') >= 0); | ||
lines = []; | ||
logger.info('lul'); | ||
assert.equal(lines.length, 0); | ||
assert.end(); | ||
}); | ||
test('prints warn/info if NODE_DEBUG', function t(assert) { | ||
var lines = []; | ||
var logger = DebugLogtron('wat', { | ||
console: { | ||
error: function log(x) { | ||
lines.push(x); | ||
} | ||
}, | ||
env: { | ||
NODE_DEBUG: 'wat' | ||
} | ||
}); | ||
logger.info('hi'); | ||
assert.equal(lines.length, 1); | ||
assert.ok(lines[0].indexOf('INFO: hi ~ null') >= 0); | ||
lines = []; | ||
logger.debug('roflcopter'); | ||
assert.equal(lines.length, 0); | ||
assert.end(); | ||
}); | ||
test('prints debug/access/trace if NODE_DEBUG verbose', function t(assert) { | ||
var lines = []; | ||
var logger = DebugLogtron('wat', { | ||
console: { | ||
error: function log(x) { | ||
lines.push(x); | ||
} | ||
}, | ||
env: { | ||
NODE_DEBUG: 'watverbose' | ||
} | ||
}); | ||
logger.debug('hi'); | ||
assert.equal(lines.length, 1); | ||
assert.ok(lines[0].indexOf('DEBUG: hi ~ null') >= 0); | ||
logger.info('hi'); | ||
assert.equal(lines.length, 2); | ||
assert.ok(lines[1].indexOf('INFO: hi ~ null') >= 0); | ||
assert.throws(function throwIt() { | ||
logger.error('hi'); | ||
}, 'hi'); | ||
assert.equal(lines.length, 3); | ||
assert.ok(lines[2].indexOf('ERROR: hi ~ null') >= 0); | ||
assert.end(); | ||
}); | ||
function allocLogger(opts) { | ||
opts = opts || {}; | ||
var logger = DebugLogtron('debuglogtrontestcode', { | ||
env: { | ||
NODE_DEBUG: 'debuglogtrontestcode ' + | ||
'debuglogtrontestcodeverbose' | ||
}, | ||
console: { | ||
error: function logStatement(msg) { | ||
logger.lines.push({ | ||
namespace: namespace, | ||
msg: msg | ||
}); | ||
}; | ||
} | ||
} | ||
}, | ||
colors: opts.colors | ||
}); | ||
logger.lines = []; | ||
process.env.NODE_DEBUG = prev; | ||
return logger; | ||
} |
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
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
22356
489
99
2
5
+ Addedchalk@1.0.0
+ Addedansi-regex@1.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.0.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedget-stdin@4.0.1(transitive)
+ Addedhas-ansi@1.0.3(transitive)
+ Addedstrip-ansi@2.0.1(transitive)
+ Addedsupports-color@1.3.1(transitive)