logwrangler
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -0,50 +1,91 @@ | ||
var _ = require('lodash'); | ||
var colors = require('colors'); | ||
var _ = require('lodash'); | ||
var logLevels = { | ||
DEBUG: 'DEBUG', | ||
INFO: 'INFO', | ||
WARN: 'WARN', | ||
ERROR: 'ERROR' | ||
}; | ||
var extendedLevels = _.extend(logLevels, { | ||
SUCCESS: 'SUCCESS' | ||
var logLevels = {}; | ||
var levelOrdering = {}; | ||
_.each(['DEBUG', 'INFO', 'WARN', 'ERROR'], function(l, i){ | ||
logLevels[l] = l; | ||
levelOrdering[l] = i; | ||
}); | ||
var levelOrdering = {}; | ||
levelOrdering[logLevels.DEBUG] = 0; | ||
levelOrdering[logLevels.INFO] = 1; | ||
levelOrdering[logLevels.WARN] = 2; | ||
levelOrdering[logLevels.ERROR] = 3; | ||
var levelTypes = _.extend({ | ||
SUCCESS: 'SUCCESS' | ||
}, logLevels); | ||
var levelToColor = {}; | ||
levelToColor[logLevels.DEBUG] = 'grey'; | ||
levelToColor[logLevels.INFO] = 'grey'; | ||
levelToColor[logLevels.WARN] = 'yellow'; | ||
levelToColor[logLevels.ERROR] = 'red'; | ||
levelToColor[logLevels.SUCCESS] = 'green'; | ||
var parseDataObject = function(data){ | ||
data = data || {}; | ||
// move the error into a property of data for consistency | ||
if(data instanceof Error){ | ||
data = { error: data }; | ||
} | ||
var consoleHandler = function(options, data){ | ||
var stringifyLogMessage = function(options, data){ | ||
var logString = []; | ||
logString.push('{' + data.level + '}'); | ||
logString.push('[' + data.ts.toString() + ']'); | ||
logString.push('(' + data.node + ')'); | ||
logString.push('(' + data.ns + ')'); | ||
logString.push('(' + data.ident + ')'); | ||
logString = [logString.join('')]; | ||
logString.push(data.message); | ||
logString = [logString.join(' - ')]; | ||
logString.push(_.isObject(data.data) ? JSON.stringify(data.data) : data.data); | ||
logString = logString.join(' :: '); | ||
if(data.error && data.error instanceof Error){ | ||
data.error = { | ||
message: data.error.message || data.error.toString(), | ||
stack: data.error.stack || '' | ||
}; | ||
} | ||
return data; | ||
}; | ||
logString = logString[levelToColor[data.type]]; | ||
return logString; | ||
}; | ||
// Logwrangler module prototype stub | ||
function LogwrangerModule(){}; | ||
LogwrangerModule.prototype.log = function(){}; | ||
if(this.levelOrdering[data.level] >= this.levelOrdering[options.level]){ | ||
var message = stringifyLogMessage(options, data); | ||
function SimpleConsoleModule(){}; | ||
SimpleConsoleModule.prototype = Object.create(LogwrangerModule.prototype); | ||
SimpleConsoleModule.prototype.getColorForType = function(type){ | ||
var color = 'grey'; | ||
switch(type){ | ||
case levelTypes.WARN: | ||
color = 'yellow'; | ||
break; | ||
case levelTypes.ERROR: | ||
color = 'red'; | ||
break; | ||
case levelTypes.SUCCESS: | ||
color = 'green'; | ||
break; | ||
case levelTypes.DEBUG: | ||
case levelTypes.INFO: | ||
default: | ||
color = 'grey'; | ||
break; | ||
} | ||
return color; | ||
}; | ||
SimpleConsoleModule.prototype.stringifyLogMessage = function(data){ | ||
var logString = []; | ||
logString.push('{' + data.level + '}'); | ||
logString.push('[' + data.ts.toString() + ']'); | ||
logString.push('(' + data.node + ')'); | ||
logString.push('(' + data.ns + ')'); | ||
logString.push('(' + data.ident + ')'); | ||
logString = [logString.join('')]; | ||
logString.push(data.message); | ||
logString = [logString.join(' - ')]; | ||
logString.push(_.isObject(data.data) ? JSON.stringify(data.data) : data.data); | ||
logString = logString.join(' :: '); | ||
logString = logString[this. getColorForType(data.type)]; | ||
return logString; | ||
}; | ||
SimpleConsoleModule.prototype.log = function(initOptions, data){ | ||
if(levelOrdering[data.level] >= levelOrdering[initOptions.level]){ | ||
console.log(this.stringifyLogMessage(data)); | ||
} | ||
}; | ||
function EnchancedConsoleModule(){}; | ||
EnchancedConsoleModule.prototype = Object.create(SimpleConsoleModule.prototype); | ||
EnchancedConsoleModule.prototype.log = function(initOptions, data){ | ||
if(levelOrdering[data.level] >= levelOrdering[initOptions.level]){ | ||
var message = this.stringifyLogMessage(data); | ||
switch(data.level){ | ||
@@ -63,111 +104,76 @@ case 'WARN': | ||
var parseDataObject = function(data){ | ||
if(!data){ | ||
return {}; | ||
} | ||
// move the error into a property of data for consistency | ||
if(data instanceof Error){ | ||
data = { | ||
error: data | ||
}; | ||
} | ||
if(data.error && data.error instanceof Error){ | ||
data.error = { | ||
message: data.error.message || data.error.toString(), | ||
stack: data.error.stack || '' | ||
}; | ||
} | ||
return data; | ||
}; | ||
var logwrangler = function(options){ | ||
function Logwrangler(initOptions){ | ||
var self = this; | ||
options = options || {}; | ||
initOptions = initOptions || {}; | ||
initOptions.level = initOptions.level || logLevels.DEBUG; | ||
self.levelToColor = levelToColor; | ||
self.levels = logLevels; | ||
self.levelOrdering = levelOrdering; | ||
self.types = extendedLevels; | ||
var outputs = []; | ||
self.overrides = {}; | ||
self.dfaults = { | ||
level: logLevels.INFO, | ||
initOptions.logOptions = _.defaults(initOptions.logOptions || {}, { | ||
level: logLevels.DEBUG, | ||
node: '', | ||
ns: '', | ||
ident: '', | ||
message: '', | ||
node: '', | ||
data: {}, | ||
}; | ||
data: {} | ||
}); | ||
//console.log('options', initOptions); | ||
var log = self.log = function(data){ | ||
var outputs = initOptions.outputs || []; | ||
data.data = parseDataObject(data.data); | ||
data = _.extend(_.cloneDeep(self.dfaults), data); | ||
data = _.extend(data, self.overrides); | ||
if(!data.type){ | ||
data.type = data.level; | ||
} | ||
data.ts = new Date(); | ||
_.each(outputs, function(output){ | ||
output.apply(self, [options, data]); | ||
}); | ||
var useModule = function(module){ | ||
outputs.push(module); | ||
}; | ||
var use = self.use = function(output){ | ||
outputs.push(output); | ||
}; | ||
useModule(new SimpleConsoleModule()); | ||
var useModule = self.useModule = function(moduleName, initParams){ | ||
var mod; | ||
try { | ||
mod = require(moduleName); | ||
} catch(e){ | ||
log({ | ||
level: logLevels.ERROR, | ||
ns: 'logwrangler', | ||
data: { | ||
error: e | ||
} | ||
}); | ||
} | ||
_.extend(self, { | ||
levels: logLevels, | ||
types: levelTypes, | ||
log: function(data){ | ||
data = data || {}; | ||
if(!mod){ | ||
return; | ||
} | ||
data = _.defaults(data, initOptions.logOptions); | ||
mod = new mod(initParams); | ||
use(mod); | ||
}; | ||
if(!data.type){ | ||
data.type = data.level; | ||
} | ||
var setOverrides = self.setOverrides = function(overrides){ | ||
overrides = overrides || {}; | ||
var validOverridFields = ['ns', 'message', 'level', 'ident', 'node']; | ||
self.overrides = _.pick(overrides, validOverridFields); | ||
}; | ||
data.data = parseDataObject(data.data); | ||
data.ts = new Date(); | ||
var copy = self.copy = function(){ | ||
return _.cloneDeep(self); | ||
}; | ||
_.each(outputs, function(out){ | ||
out.log(initOptions, data); | ||
}); | ||
setOverrides(options.overrides); | ||
use(consoleHandler); | ||
}, | ||
use: useModule, | ||
clone: function(){ | ||
return _.cloneDeep(self); | ||
}, | ||
setLogOption: function(name, value){ | ||
initOptions.logOptions[key] = value; | ||
}, | ||
setLogOptions: function(values){ | ||
_.each(options, function(val, key){ | ||
initOptions.logOptions[key] = val; | ||
}); | ||
} | ||
}); | ||
return self; | ||
}; | ||
var parentInst; | ||
var inst; | ||
module.exports = { | ||
create: function(options, forceNew){ | ||
if(inst && !forceNew){ | ||
return inst; | ||
} | ||
exports.levels = logLevels; | ||
exports.types = levelTypes; | ||
exports.LogwrangerModule = LogwrangerModule; | ||
exports.SimpleConsoleModule = SimpleConsoleModule; | ||
exports.EnchancedConsoleModule = EnchancedConsoleModule; | ||
inst = new logwrangler(options); | ||
exports.create = function(initOptions, forceNew){ | ||
if(inst && !forceNew){ | ||
return inst; | ||
}, | ||
levels: logLevels | ||
} | ||
inst = new Logwrangler(initOptions); | ||
return inst; | ||
}; |
{ | ||
"name": "logwrangler", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"description": "Wrangle up your logging", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,7 +0,8 @@ | ||
var logwrangler = require('../index'); | ||
var logwrangler = require('../lib/index'); | ||
var logger = logwrangler.create({ | ||
level: logwrangler.levels.DEBUG, | ||
}); | ||
console.log(logger); | ||
logger.useModule('myCoolMod', {}); | ||
//logger.useModule('myCoolMod', {}); | ||
@@ -17,2 +18,3 @@ logger.log({ | ||
logger.log({ | ||
@@ -62,7 +64,2 @@ level: logger.levels.ERROR, | ||
logger.setOverrides({ | ||
ns: 'some default ns', | ||
level: logger.levels.INFO, | ||
node: 'datacenter-1' | ||
}); | ||
logger.log({ | ||
@@ -72,10 +69,3 @@ message: 'test' | ||
logger.use(function(options, data){ | ||
console.log(options); | ||
console.log('------------------------\ncustom handler') | ||
console.log(data); | ||
console.log('------------------------') | ||
}); | ||
var copy = logger.copy(); | ||
var copy = logger.clone(); | ||
copy.log({ | ||
@@ -89,1 +79,2 @@ message: 'copy' | ||
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
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
9543
0
210
1