Skip to content

Commit

Permalink
Added replica sets support
Browse files Browse the repository at this point in the history
  • Loading branch information
yurijmikhalevich committed Jul 5, 2014
1 parent f098a0f commit 59e29f3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ The MongoDB transport takes the following options. 'db' is required:
* __storeHost:__ Boolean indicating if you want to store machine hostname in logs entry, if set to true it populates MongoDB entry with 'hostname' field, which stores os.hostname() value.
* __ssl:__ Boolean indicating if you want to use SSL connections or not.
* __authDb:__ Authentication database object.
* __dbUri:__ Alternative way of specifying database connection data. Note, that __replica sets are unsupported__. If you specify a replica set or multiple databases, will be used first database connection data.
* __replSet:__ Replica set name.
* __hosts:__ Array of replica set hosts (in format ```{host: 'string', port: 'number'}```)
* __dbUri:__ Alternative way of specifying database connection data. Supported specifying database, host, port, username, password and replica sets.

*Notice:* __db__ is required. You should specify it directly or in __dbUri__.

*ReplicaSet Notice:* If you use replica set, __db__, __replSet__ and __hosts__ are required. They may also be specified in __dbUri__.

*Metadata:* Logged as a native JSON object.

## Querying and streaming logs
Expand Down
27 changes: 22 additions & 5 deletions lib/winston-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ var MongoDB = exports.MongoDB = function (options) {
if (options.dbUri) {
var uriOptions = muri(options.dbUri);
options.db = uriOptions.db;
if (uriOptions.hosts && uriOptions.hosts[0]) {
if (uriOptions.options.replicaSet) {
options.replSet = uriOptions.options.replicaSet;
options.hosts = uriOptions.hosts;
} else if (uriOptions.hosts && uriOptions.hosts[0]) {
options.host = uriOptions.hosts[0].host;
options.port = uriOptions.hosts[0].port;
}
Expand All @@ -46,6 +49,8 @@ var MongoDB = exports.MongoDB = function (options) {
this.db = options.db;
this.host = options.host || 'localhost';
this.port = options.port || mongodb.Connection.DEFAULT_PORT;
this.replSet = options.replSet || null;
this.hosts = options.hosts || null;
this.collection = options.collection || 'logs';
this.safe = options.safe || true;
this.level = options.level || 'info';
Expand Down Expand Up @@ -73,10 +78,22 @@ var MongoDB = exports.MongoDB = function (options) {
this.timeoutId = null;
this.pending = [];

this.server = new mongodb.Server(this.host, this.port, {
ssl: this.ssl
});

if (this.replSet) {
var servers = [];
this.hosts.forEach(function(host) {
servers.push(new mongodb.Server(host.host || 'localhost',
host.port || mongodb.Connection.DEFAULT_PORT));
});
this.server = new mongodb.ReplSet(servers, {
rs_name: this.replSet,
ssl: this.ssl
});
} else {
this.server = new mongodb.Server(this.host, this.port, {
ssl: this.ssl
});
}

this.client = new mongodb.MongoClient(this.server, {
native_parser: this.nativeParser,
safe: this.safe
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-mongodb",
"version": "0.4.5",
"version": "0.4.6",
"description": "A MongoDB transport for winston",
"author": "Charlie Robbins <[email protected]>",
"contributors": [
Expand Down
24 changes: 23 additions & 1 deletion test/winston-mongodb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,30 @@ var path = require('path'),
MongoDB = require('../lib/winston-mongodb').MongoDB;

vows.describe('winston-mongodb').addBatch({
"An instance of the MongoDB Transport": transport(MongoDB, {
'An instance of the MongoDB Transport': transport(MongoDB, {
db: 'winston',
keepAlive: 1000
})
}).export(module);


//vows.describe('winston-mongodb').addBatch({
// 'Test MongoDB with replica set': transport(MongoDB, {
// db: 'winston',
// keepAlive: 1000,
// replSet: 'rs0',
// hosts: [
// {port: 27018},
// {port: 27019},
// {port: 27020}
// ]
// })
//}).export(module);


//vows.describe('winston-mongodb').addBatch({
// 'Test MongoDB with replica set': transport(MongoDB, {
// keepAlive: 1000,
// dbUri: 'mongodb://localhost:27018,localhost:27019,localhost:27020/winston?replicaSet=rs0'
// })
//}).export(module);

0 comments on commit 59e29f3

Please sign in to comment.