Merge pull request #231 from eurekaa/buffered-logger
added getBufferedLogger function.
This commit is contained in:
commit
c03185b563
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ node_modules
|
|||||||
.bob/
|
.bob/
|
||||||
test/streams/test-rolling-file-stream*
|
test/streams/test-rolling-file-stream*
|
||||||
test/streams/test-rolling-stream-with-existing-files*
|
test/streams/test-rolling-stream-with-existing-files*
|
||||||
|
.idea
|
||||||
|
@ -70,6 +70,29 @@ function hasLogger(logger) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getBufferedLogger(categoryName) {
|
||||||
|
var base_logger = getLogger(categoryName);
|
||||||
|
var logger = {};
|
||||||
|
logger.temp = [];
|
||||||
|
logger.target = base_logger;
|
||||||
|
logger.flush = function () {
|
||||||
|
for (var i = 0; i < logger.temp.length; i++) {
|
||||||
|
var log = logger.temp[i];
|
||||||
|
logger.target[log.level](log.message);
|
||||||
|
delete logger.temp[i];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
logger.trace = function (message) { logger.temp.push({level: 'trace', message: message}); };
|
||||||
|
logger.debug = function (message) { logger.temp.push({level: 'debug', message: message}); };
|
||||||
|
logger.info = function (message) { logger.temp.push({level: 'info', message: message}); };
|
||||||
|
logger.warn = function (message) { logger.temp.push({level: 'warn', message: message}); };
|
||||||
|
logger.error = function (message) { logger.temp.push({level: 'error', message: message}); };
|
||||||
|
logger.fatal = function (message) { logger.temp.push({level: 'fatal', message: message}); };
|
||||||
|
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a logger instance. Instance is cached on categoryName level.
|
* Get a logger instance. Instance is cached on categoryName level.
|
||||||
* @param {String} categoryName name of category to log to.
|
* @param {String} categoryName name of category to log to.
|
||||||
@ -375,6 +398,7 @@ function shutdown(cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getBufferedLogger: getBufferedLogger,
|
||||||
getLogger: getLogger,
|
getLogger: getLogger,
|
||||||
getDefaultLogger: getDefaultLogger,
|
getDefaultLogger: getDefaultLogger,
|
||||||
hasLogger: hasLogger,
|
hasLogger: hasLogger,
|
||||||
|
@ -32,6 +32,72 @@ function setupConsoleTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vows.describe('log4js').addBatch({
|
vows.describe('log4js').addBatch({
|
||||||
|
|
||||||
|
'getBufferedLogger': {
|
||||||
|
topic: function () {
|
||||||
|
var log4js = require('../lib/log4js');
|
||||||
|
log4js.clearAppenders();
|
||||||
|
var logger = log4js.getBufferedLogger('tests');
|
||||||
|
return logger;
|
||||||
|
},
|
||||||
|
|
||||||
|
'should take a category and return a logger': function (logger) {
|
||||||
|
assert.equal(logger.target.category, 'tests');
|
||||||
|
assert.isFunction(logger.flush);
|
||||||
|
assert.isFunction(logger.trace);
|
||||||
|
assert.isFunction(logger.debug);
|
||||||
|
assert.isFunction(logger.info);
|
||||||
|
assert.isFunction(logger.warn);
|
||||||
|
assert.isFunction(logger.error);
|
||||||
|
assert.isFunction(logger.fatal);
|
||||||
|
},
|
||||||
|
|
||||||
|
'cache events': {
|
||||||
|
topic: function () {
|
||||||
|
var log4js = require('../lib/log4js');
|
||||||
|
log4js.clearAppenders();
|
||||||
|
var logger = log4js.getBufferedLogger('tests1');
|
||||||
|
var events = [];
|
||||||
|
logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
|
||||||
|
logger.debug("Debug event");
|
||||||
|
logger.trace("Trace event 1");
|
||||||
|
logger.trace("Trace event 2");
|
||||||
|
logger.warn("Warning event");
|
||||||
|
logger.error("Aargh!", new Error("Pants are on fire!"));
|
||||||
|
logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
|
||||||
|
return events;
|
||||||
|
},
|
||||||
|
|
||||||
|
'should not emit log events if .flush() is not called.': function (events) {
|
||||||
|
assert.equal(events.length, 0);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
'log events after flush() is called': {
|
||||||
|
topic: function () {
|
||||||
|
var log4js = require('../lib/log4js');
|
||||||
|
log4js.clearAppenders();
|
||||||
|
var logger = log4js.getBufferedLogger('tests2');
|
||||||
|
logger.target.setLevel("TRACE");
|
||||||
|
var events = [];
|
||||||
|
logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
|
||||||
|
logger.debug("Debug event");
|
||||||
|
logger.trace("Trace event 1");
|
||||||
|
logger.trace("Trace event 2");
|
||||||
|
logger.warn("Warning event");
|
||||||
|
logger.error("Aargh!", new Error("Pants are on fire!"));
|
||||||
|
logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
|
||||||
|
logger.flush();
|
||||||
|
return events;
|
||||||
|
},
|
||||||
|
|
||||||
|
'should emit log events when .flush() is called.': function (events) {
|
||||||
|
assert.equal(events.length, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
'getLogger': {
|
'getLogger': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var log4js = require('../lib/log4js');
|
var log4js = require('../lib/log4js');
|
||||||
|
Loading…
Reference in New Issue
Block a user