Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CouchDB Appender #88

Closed
wants to merge 1 commit into from

3 participants

@RobinQu

Using couchdb as log appender.

Defaults to store all log info in localhost:5984. Pass db info by:

log4js.addAppender(require('../lib/appenders/couch').appender({
  host: "localhost",
  port: 5984,
  protocol: "http",
  db: "logs"
}), 'default-settings');
@nomiddlename
Owner

Thanks for this - the idea is great, and I'd like to accept the pull request, but I have some issues. I've added comments to the commit.

@bernharduw

Any progress on this pull request? Would be nice if you added couchdb as an appender.

@RobinQu

Many logging utilities have already got this feature, including the one I made and winston.

@bernharduw

Cool, thanks! We'll look into the other repos. Currently we are using log4js in all our projects, but I'm open to new options.

@nomiddlename

I've no problem with supporting CouchDB, I just had some issues with the implementation in this appender.

@bernharduw

Sure, I was aware of that! Unfortunately, I can't see the comments on the commit. Comments seem to be private between you and @RobinQu.

@nomiddlename

Hmm - I can't see the comments any more either. They must have got lost in one of the github redesigns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 8, 2012
  1. @RobinQu

    couch appender

    RobinQu authored
This page is out of date. Refresh to see the latest.
View
53 lib/appenders/couch.js
@@ -0,0 +1,53 @@
+var nano = require("nano"),
+ util = require("util"),
+ async = require("async");
+
+var C = {//backup of console.log; in case of infinity loops after console replacement
+ log: function() {
+ process.stdout.write(util.format.apply(this, arguments) + '\n');
+ },
+ error: function() {
+ process.stderr.write(util.format.apply(this, arguments) + '\n');
+ }
+};
+
+function createAppender(opts) {
+ var db, endpoint;
+
+ opts = opts || {};
+ opts.host = opts.host || "localhost";
+ opts.secured = (typeof opts.secure == "undefined") ? false : opts.secured;
+ opts.port = 5984;
+ opts.protocol = opts.secured ? "https" : "http";
+ opts.db = opts.db || "logs";
+ endpoint = [opts.protocol, "://", opts.host, ":", opts.port, "/", opts.db].join("");
+ db = nano(endpoint);
+
+ return function(loggingEvent) {
+ var logs, m, d;
+
+ d = new Date(loggingEvent.startTime);
+ m = {
+ date: [d.getFullYear(), d.getMonth()+1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()],
+ category: loggingEvent.categoryName
+ };
+ logs = loggingEvent.data.map(function(item, i) {
+ return {
+ date: m.date,
+ category: m.category,
+ data: item
+ };
+ });
+ db.bulk({docs: logs}, function(e) {
+ if(e) C.error(e);
+ });
+ };
+}
+
+function configure(config, options) {
+ var opt = options.couch ? options.couch : config;
+ return createAppender(opt);
+}
+
+exports.appender = createAppender;
+exports.configure = configure;
View
3  package.json
@@ -32,6 +32,7 @@
"vows": "0.6.2",
"sandboxed-module": "0.1.3",
"hook.io": "0.8.10",
- "underscore": "1.2.1"
+ "underscore": "1.2.1",
+ "nano": "*"
}
}
View
22 test/couch-test.js
@@ -0,0 +1,22 @@
+var path = require('path')
+, log4js = require('../lib/log4js')
+, assert = require('assert')
+, nano = require('nano')
+, util = require('util');
+
+
+var logger = log4js.getLogger('default-settings')
+, db = nano("http://localhost:5984/logs")
+, feed = db.follow({since: "now", include_doc: true});
+
+ log4js.clearAppenders();
+ log4js.addAppender(require('../lib/appenders/couch').appender(), 'default-settings');
+
+ feed.once('change', function(change) {
+ var db = nano("http://localhost:5984/logs");
+ db.get(change.id, function(e, doc) {
+ console.log(doc);
+ });
+ });
+ feed.follow();
+ logger.info("This should be in local couchdb.");
View
36 test/local-couchAppender-test.js
@@ -0,0 +1,36 @@
+var vows = require('vows')
+, fs = require('fs')
+, path = require('path')
+, log4js = require('../lib/log4js')
+, assert = require('assert')
+, nano = require('nano')
+, util = require('util');
+
+
+log4js.clearAppenders();
+
+vows.describe('log4js couchAppender').addBatch({
+
+ 'with default couchAppender settings': {
+ topic: function() {
+ var that = this
+ ,logger = log4js.getLogger('default-settings')
+ , db = nano("http://localhost:5984/logs")
+ , feed = db.follow({since: "now", include_doc: true});
+
+ log4js.clearAppenders();
+ log4js.addAppender(require('../lib/appenders/couch').appender(), 'default-settings');
+
+ feed.once('change', that.callback);
+ feed.follow();
+ logger.info("This should be in local couchdb.");
+
+ },
+ 'should write log messages to the couchdb': function(change) {
+ var db = nano("http://localhost:5984/logs");
+ db.get(change.id, function(e, doc) {
+ assert.include(doc.data, "This should be in local couchdb.");
+ });
+ }
+ }
+}).export(module);
Something went wrong with that request. Please try again.