Permalink
Browse files

Moved MongoDbAppender to different package

Refactored the filtering part of Appender to Barrier, and gave every Appender a Barrier
  • Loading branch information...
1 parent 3939b7a commit c8b3df78dcbd5c22026876667c9a669182b671e9 @StevenLooman committed Sep 20, 2012
@@ -1,15 +1,16 @@
var FilterReply = require('./filter_reply');
-function Appender() {
+function Barrier() {
+ this.filters = [];
}
-Appender.prototype.addFilter = function(filter) {
+Barrier.prototype.addFilter = function(filter) {
this.filters.push(filter);
};
-Appender.prototype.shouldAppend = function(le) {
+Barrier.prototype.passes = function(le) {
var i;
var filter, reply;
for (i = 0; i < this.filters.length; ++i) {
@@ -26,18 +27,5 @@ Appender.prototype.shouldAppend = function(le) {
return true;
};
-Appender.prototype.doAppend = function(le) {
- if (!this.shouldAppend(le)) {
- return;
- }
-
- this.append(le);
-};
-
-
-Appender.prototype.getName = function() {
- return this.name;
-};
-
-module.exports = Appender;
+module.exports = Barrier;
@@ -1,17 +1,17 @@
-var path = require('path');
-var fs = require('fs');
+var path = require('path');
+var fs = require('fs');
var DOMParser = require('xmldom').DOMParser;
+var Barrier = require('./barrier');
+var Levels = require('./levels');
+var Logger = require('./logger');
var LoggerFactory = require('./logger_factory');
-var Logger = require('./logger');
-var Levels = require('./levels');
var knownAppenders = {
'console_appender' : require('./console_appender'),
'file_appender' : require('./file_appender'),
- 'rolling_file_appender': require('./rolling_file_appender'),
- 'mongodb_appender' : require('./mongodb_appender')
+ 'rolling_file_appender': require('./rolling_file_appender')
};
var knownEncoders = {
@@ -97,7 +97,21 @@ function parseAppenders(appenderNodes) {
function parseAppender(node) {
var name = node.getAttribute('name');
var type = node.getAttribute('type');
+ var module = node.getAttribute('module');
+
+ // try loading the Appender externally
+ if (module) {
+ try {
+ knownAppenders[module] = require(module);
+ type = module;
+ } catch(err) {
+ console.error('blammo.ConfigurationReader: unable to load module: ' + module);
+ throw new Error('blammo.ConfigurationReader: unable to load module: ' + module);
+ }
+ }
+
+ // build Appender
var encoderNode = getChildByTagName(node, 'encoder');
var encoder = parseEncoder(encoderNode);
@@ -111,13 +125,15 @@ function parseAppender(node) {
}
var appender = new knownAppenders[type](name, encoder, layout, config);
+ appender.barrier = new Barrier();
+ // add filters
var filtersNode = getChildByTagName(node, 'filters');
var filters = parseFilters(filtersNode);
filters.forEach(function(filter) {
- appender.addFilter(filter);
+ appender.barrier.addFilter(filter);
});
@@ -1,8 +1,3 @@
-var util = require('util');
-
-var Appender = require('./appender');
-
-
/**
* Build a ConsoleAppender
*
@@ -12,14 +7,9 @@ function ConsoleAppender(name, encoder, layout) {
this.name = name;
this.encoder = encoder;
this.layout = layout;
-
- this.filters = [];
}
-util.inherits(ConsoleAppender, Appender);
-
-
ConsoleAppender.prototype.append = function(e) {
var str = this.layout.doLayout(e);
var encodedStr = this.encoder.doEncode(str);
@@ -1,8 +1,5 @@
-var util = require('util');
var fs = require('fs');
-var Appender = require('./appender');
-
/**
* Build a FileAppender
@@ -14,14 +11,9 @@ function FileAppender(name, encoder, layout, config) {
this.encoder = encoder;
this.layout = layout;
this.filename = config.filename;
-
- this.filters = [];
}
-util.inherits(FileAppender, Appender);
-
-
FileAppender.prototype.append = function(e) {
var str = this.layout.doLayout(e);
var encodedStr = this.encoder.doEncode(str) + '\n';
View
@@ -2,12 +2,11 @@ module.exports.LoggerFactory = require('./logger_factory');
module.exports.Levels = require('./levels');
module.exports.Logger = require('./logger');
module.exports.LoggingEvent = require('./logging_event');
+module.exports.Barrier = require('./barrier');
-module.exports.Appender = require('./appender');
module.exports.ConsoleAppender = require('./console_appender');
module.exports.FileAppender = require('./file_appender');
module.exports.RollingFileAppender = require('./rolling_file_appender');
-module.exports.MongoDbAppender = require('./mongodb_appender');
module.exports.DummyEncoder = require('./dummy_encoder');
View
@@ -1,5 +1,6 @@
-var LoggingEvent = require('./logging_event');
+var Barrier = require('./barrier');
var Levels = require('./levels');
+var LoggingEvent = require('./logging_event');
var utils = require('./utils');
@@ -41,6 +42,10 @@ Logger.prototype.getLevel = function() {
* Add an Appender to this Logger
*/
Logger.prototype.addAppender = function(appender) {
+ if (!appender.barrier) { // XXX: HACK: attach a barrier if not exists
+ appender.barrier = new Barrier();
+ }
+
this.appenders.push(appender);
};
@@ -112,7 +117,9 @@ Logger.prototype.log = function(level, message) {
var le = new LoggingEvent(this.name, level, message);
this.getAppenders().forEach(function(appender) {
- appender.doAppend(le);
+ if (appender.barrier.passes(le)) {
+ appender.append(le);
+ }
});
};
@@ -1,70 +0,0 @@
-var mongodb = require('mongodb');
-var util = require('util');
-
-var Appender = require('./appender');
-
-
-/**
- * MongoDb appender for Blammo
- */
-function MongoDbAppender(name, encoder, layout, options) {
- this.name = name;
- this.encoder = encoder;
- this.layout = layout;
-
- this.filters = [];
-
- this.connect(options.database, options.hostname, parseInt(options.port, 10), options.collection);
-}
-
-
-util.inherits(MongoDbAppender, Appender);
-
-
-MongoDbAppender.prototype.connect = function(database, hostname, port, collection) {
- var db = new mongodb.Db(database, new mongodb.Server(hostname, port, { auto_reconnect: true }));
- var self = this;
-
- function collectionCb(err, col) {
- if (err) {
- console.error('blammo.MongoDbAppender: ' + err);
- return;
- }
-
- self.collection = col;
- }
-
- function openCb(err, db) {
- if (err) {
- console.error('blammo.MongoDbAppender: ' + err);
- return;
- }
-
- db.collection(collection, collectionCb);
- }
-
- db.open(openCb);
-};
-
-
-/**
- * Append a LoggingEvent to the earlier specified collection
- */
-MongoDbAppender.prototype.append = function(e) {
- if (!this.collection) {
- // something broke?
- console.log('no collection');
- return;
- }
-
- function insertCb(err, doc) {
- if (err) {
- console.error(err);
- }
- }
-
- this.collection.insert(e, insertCb);
-};
-
-
-module.exports = MongoDbAppender;
@@ -1,7 +1,5 @@
-var util = require('util');
var fs = require('fs');
-var Appender = require('./appender');
var utils = require('./utils');
@@ -10,14 +8,9 @@ function RollingFileAppender(name, encoder, layout, config) {
this.encoder = encoder;
this.layout = layout;
this.filename = config.filename;
-
- this.filters = [];
}
-util.inherits(RollingFileAppender, Appender);
-
-
RollingFileAppender.prototype.append = function(e) {
var str = this.layout.doLayout(e);
var encodedStr = this.encoder.doEncode(str) + '\n';
View
@@ -12,12 +12,11 @@
],
"dependencies": {
- "xmldom": "0.1.11",
- "mongodb": "1.1.5"
+ "xmldom": "0.1.11"
},
"devDependencies": {
"mocha": "1.2.2",
- "mocha-lcov-reporter": "0.0.1"
+ "mocha-lcov-reporter": "0.0.1"
},
"directories": {
"lib": "./lib",
View
@@ -0,0 +1,45 @@
+var blammo = require('..');
+var assert = require('assert');
+
+describe('Barrier', function() {
+ describe('#passes()', function() {
+ it('should pass when it has no filters', function() {
+ var barrier = new blammo.Barrier();
+
+ assert.ok(barrier.passes());
+ });
+
+ it('should pass when a filter ACCEPTs it', function() {
+ var barrier = new blammo.Barrier();
+ var filter = new blammo.LevelFilter('TRACE');
+ barrier.addFilter(filter);
+
+ var le = new blammo.LoggingEvent('test', blammo.Levels.TRACE, 'dummy message');
+ var r = barrier.passes(le);
+
+ assert.ok(r);
+ });
+
+ it('should pass when a filter NEUTRALs it', function() {
+ var barrier = new blammo.Barrier();
+ var filter = new blammo.LevelFilter('TRACE');
+ barrier.addFilter(filter);
+
+ var le = new blammo.LoggingEvent('test', blammo.Levels.DEBUG, 'dummy message');
+ var r = barrier.passes(le);
+
+ assert.ok(r);
+ });
+
+ it('should not pass when filter DENYs it', function() {
+ var barrier = new blammo.Barrier();
+ var filter = new blammo.ThresholdFilter('WARN');
+ barrier.addFilter(filter);
+
+ var le = new blammo.LoggingEvent('test', blammo.Levels.DEBUG, 'dummy message');
+ var r = barrier.passes(le);
+
+ assert.ok(!r);
+ });
+ });
+});
View
@@ -188,8 +188,7 @@ describe('Logger', function() {
describe('auto tracing', function() {
it('should give a TRACE log message on entry', function() {
function T() {}
- T.prototype.f1 = function(p1, p2) {
- };
+ T.prototype.f1 = function(p1, p2) { };
var t = new T();
var logger = new blammo.Logger('test');
@@ -206,8 +205,7 @@ describe('Logger', function() {
it('should give a TRACE log message on exit', function() {
function T() {}
- T.prototype.f1 = function(p1, p2) {
- };
+ T.prototype.f1 = function(p1, p2) { };
var t = new T();
var logger = new blammo.Logger('test');
@@ -224,8 +222,7 @@ describe('Logger', function() {
it('should give a TRACE log message on entry and exit', function() {
function T() {}
- T.prototype.f1 = function(p1, p2) {
- };
+ T.prototype.f1 = function(p1, p2) { };
var t = new T();
var logger = new blammo.Logger('test');
@@ -243,4 +240,3 @@ describe('Logger', function() {
});
});
});
-
@@ -14,7 +14,7 @@ TestAppender.prototype.getName = function() {
};
-TestAppender.prototype.doAppend = function(e) {
+TestAppender.prototype.append = function(e) {
this.events.push(e);
};

0 comments on commit c8b3df7

Please sign in to comment.