Permalink
Browse files

Added MongoDbAppender

  • Loading branch information...
1 parent 1bea2c3 commit 6fd6504e8f3eafcb51077b38f59078532a316b57 @StevenLooman committed Sep 19, 2012
Showing with 128 additions and 2 deletions.
  1. +14 −0 TODO.md
  2. +26 −0 blammo_mongodb.xml
  3. +3 −1 lib/configuration_reader.js
  4. +13 −0 lib/dummy_layout.js
  5. +70 −0 lib/mongodb_appender.js
  6. +2 −1 package.json
View
14 TODO.md
@@ -0,0 +1,14 @@
+TODO
+====
+
+MongoDbAppender has to be 'yielded' before it connects
+------------------------------------------------------
+Due to the workings of NodeJs/v8, MongoDb will not connect/get the colleciton until the main 'thread' is 'yielded' (i.e., until is has nothing more to do and another callback/timeout will be fired.) This means that if the loggers/appenders are built, and the main 'thread' is running from the start, the MongoDbAppender will not be connected and thus the appender will have nothing to append to. This should somehow be fixed.
+
+MongoDbAppender should be in its own module
+-------------------------------------------
+MongoDbAppender should be in its own module. Blammo should be as lightweight as possible. Non-core functionality, such as the MongoDbAppender, should be in its own module.
+
+However, due to cyclic dependencies, it is not possible to put the MongoDbAppender in a separate module. The MongoDbAppender would be loaded by the configuration_reader.js file on load time of Blammo. The filtering part of the Appenders is required by Blammo, thus the MongoDbAppender has to require Blammo. Hence, a cycling dependency.
+
+To fix this issue, the filtering part of the Appender has to be separated from the Appender. I.e., a bit of refactoring is in place. Or, at runtime, the filtering mechanism could be mixed in as well.
View
@@ -0,0 +1,26 @@
+<configuration>
+ <appender name="console" type="console_appender">
+ <encoder type="dummy_encoder"/>
+ <layout type="pattern_layout">%timestamp|%pid|%logger|%level|%message</layout>
+ </appender>
+
+ <appender name="mongodb" type="mongodb_appender">
+ <encoder type="dummy_encoder"/>
+ <layout type="dummy_layout" />
+
+ <config>
+ <hostname>localhost</hostname>
+ <port>27017</port>
+ <database>blammo</database>
+ <collection>log</collection>
+ </config>
+ </appender>
+
+ <logger name="logger1">
+ <appender-ref ref="console" />
+ <appender-ref ref="mongodb" />
+ </logger>
+
+ <root level="trace">
+ </root>
+</configuration>
@@ -10,14 +10,16 @@ var Levels = require('./levels');
var knownAppenders = {
'console_appender' : require('./console_appender'),
'file_appender' : require('./file_appender'),
- 'rolling_file_appender': require('./rolling_file_appender')
+ 'rolling_file_appender': require('./rolling_file_appender'),
+ 'mongodb_appender' : require('./mongodb_appender')
};
var knownEncoders = {
'dummy_encoder': require('./dummy_encoder')
};
var knownLayouts = {
+ 'dummy_layout' : require('./dummy_layout'),
'pattern_layout': require('./pattern_layout'),
'json_layout' : require('./json_layout')
};
View
@@ -0,0 +1,13 @@
+/**
+ * Dummy Layout
+ */
+function DummyLayout() {
+}
+
+
+DummyLayout.prototype.doLayout = function(le) {
+ return le;
+};
+
+
+module.exports = DummyLayout;
View
@@ -0,0 +1,70 @@
+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;
View
@@ -12,7 +12,8 @@
],
"dependencies": {
- "xmldom": "0.1.11"
+ "xmldom": "0.1.11",
+ "mongodb": "1.1.5"
},
"devDependencies": {
"mocha": "1.2.2",

0 comments on commit 6fd6504

Please sign in to comment.