Skip to content

Commit 59e29f3

Browse files
Added replica sets support
1 parent f098a0f commit 59e29f3

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ The MongoDB transport takes the following options. 'db' is required:
3838
* __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.
3939
* __ssl:__ Boolean indicating if you want to use SSL connections or not.
4040
* __authDb:__ Authentication database object.
41-
* __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.
41+
* __replSet:__ Replica set name.
42+
* __hosts:__ Array of replica set hosts (in format ```{host: 'string', port: 'number'}```)
43+
* __dbUri:__ Alternative way of specifying database connection data. Supported specifying database, host, port, username, password and replica sets.
4244

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

47+
*ReplicaSet Notice:* If you use replica set, __db__, __replSet__ and __hosts__ are required. They may also be specified in __dbUri__.
48+
4549
*Metadata:* Logged as a native JSON object.
4650

4751
## Querying and streaming logs

lib/winston-mongodb.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ var MongoDB = exports.MongoDB = function (options) {
2626
if (options.dbUri) {
2727
var uriOptions = muri(options.dbUri);
2828
options.db = uriOptions.db;
29-
if (uriOptions.hosts && uriOptions.hosts[0]) {
29+
if (uriOptions.options.replicaSet) {
30+
options.replSet = uriOptions.options.replicaSet;
31+
options.hosts = uriOptions.hosts;
32+
} else if (uriOptions.hosts && uriOptions.hosts[0]) {
3033
options.host = uriOptions.hosts[0].host;
3134
options.port = uriOptions.hosts[0].port;
3235
}
@@ -46,6 +49,8 @@ var MongoDB = exports.MongoDB = function (options) {
4649
this.db = options.db;
4750
this.host = options.host || 'localhost';
4851
this.port = options.port || mongodb.Connection.DEFAULT_PORT;
52+
this.replSet = options.replSet || null;
53+
this.hosts = options.hosts || null;
4954
this.collection = options.collection || 'logs';
5055
this.safe = options.safe || true;
5156
this.level = options.level || 'info';
@@ -73,10 +78,22 @@ var MongoDB = exports.MongoDB = function (options) {
7378
this.timeoutId = null;
7479
this.pending = [];
7580

76-
this.server = new mongodb.Server(this.host, this.port, {
77-
ssl: this.ssl
78-
});
79-
81+
if (this.replSet) {
82+
var servers = [];
83+
this.hosts.forEach(function(host) {
84+
servers.push(new mongodb.Server(host.host || 'localhost',
85+
host.port || mongodb.Connection.DEFAULT_PORT));
86+
});
87+
this.server = new mongodb.ReplSet(servers, {
88+
rs_name: this.replSet,
89+
ssl: this.ssl
90+
});
91+
} else {
92+
this.server = new mongodb.Server(this.host, this.port, {
93+
ssl: this.ssl
94+
});
95+
}
96+
8097
this.client = new mongodb.MongoClient(this.server, {
8198
native_parser: this.nativeParser,
8299
safe: this.safe

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "winston-mongodb",
3-
"version": "0.4.5",
3+
"version": "0.4.6",
44
"description": "A MongoDB transport for winston",
55
"author": "Charlie Robbins <[email protected]>",
66
"contributors": [

test/winston-mongodb-test.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,30 @@ var path = require('path'),
1515
MongoDB = require('../lib/winston-mongodb').MongoDB;
1616

1717
vows.describe('winston-mongodb').addBatch({
18-
"An instance of the MongoDB Transport": transport(MongoDB, {
18+
'An instance of the MongoDB Transport': transport(MongoDB, {
1919
db: 'winston',
2020
keepAlive: 1000
2121
})
2222
}).export(module);
23+
24+
25+
//vows.describe('winston-mongodb').addBatch({
26+
// 'Test MongoDB with replica set': transport(MongoDB, {
27+
// db: 'winston',
28+
// keepAlive: 1000,
29+
// replSet: 'rs0',
30+
// hosts: [
31+
// {port: 27018},
32+
// {port: 27019},
33+
// {port: 27020}
34+
// ]
35+
// })
36+
//}).export(module);
37+
38+
39+
//vows.describe('winston-mongodb').addBatch({
40+
// 'Test MongoDB with replica set': transport(MongoDB, {
41+
// keepAlive: 1000,
42+
// dbUri: 'mongodb://localhost:27018,localhost:27019,localhost:27020/winston?replicaSet=rs0'
43+
// })
44+
//}).export(module);

0 commit comments

Comments
 (0)