Comparing version 0.3.0 to 0.3.1
@@ -361,3 +361,29 @@ /** | ||
/** | ||
* getInstance: get a singleton instance of the logger for a given config | ||
* | ||
* @param config | ||
* (object) The logger config (see Logger constructor) | ||
* | ||
*/ | ||
var instances = {}; | ||
var getInstance = function getInstance(config) { | ||
config = config || {}; | ||
// make an instance key for this config | ||
var configKey = []; | ||
Object.keys(config).sort().forEach(function(key) { | ||
configKey.push(key + '=' + config[key]); | ||
}); | ||
// stringify the array | ||
configKey = configKey.join(','); | ||
if (!instances[configKey]) { | ||
instances[configKey] = new Logger(config); | ||
} | ||
return instances[configKey]; | ||
}; | ||
module.exports.getInstance = getInstance; | ||
/* | ||
@@ -364,0 +390,0 @@ * CEF Helpers |
{ | ||
"name": "cef", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"main" : "lib/cef.js", | ||
@@ -5,0 +5,0 @@ "author": "Jed Parsons <https://github.com/jedp>", |
@@ -35,3 +35,4 @@ # node-cef: A CEF and Syslog Logging Library | ||
var logger = new cef.Logger(config); | ||
// Get a singleton instance for this config | ||
var logger = cef.getInstance(config); | ||
@@ -38,0 +39,0 @@ logger.info({signature: "Bflat", name: "Out of tune"}); |
@@ -84,4 +84,25 @@ var vows = require('vows'); | ||
} | ||
}). | ||
addBatch({ | ||
"getInstance": { | ||
topic: function() { | ||
var config = { | ||
vendor: 'Foo', | ||
product: 'Bar', | ||
version: '0.1-baz' | ||
}; | ||
return [config, cef.getInstance(config)]; | ||
}, | ||
"returns a singleton": function(tuple) { | ||
var config = tuple[0]; | ||
var firstLogger = tuple[1]; | ||
assert(cef.getInstance(config) === firstLogger); | ||
assert(cef.getInstance() !== firstLogger); | ||
} | ||
} | ||
}); | ||
if (process.argv[1] === __filename) { | ||
@@ -88,0 +109,0 @@ suite.run(); |
38201
1029
202