Skip to content

Commit 1f8a994

Browse files
Version 1.4.0: added tryReconnect option
1 parent a98daa6 commit 1f8a994

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ MongoDB transports.
5353
new log collection as capped, defaults to false.
5454
* __cappedSize:__ Size of logs capped collection in bytes, defaults to 10000000.
5555
* __cappedMax:__ Size of logs capped collection in number of documents.
56+
* __tryReconnect:__ Will try to reconnect to the database in case of fail during
57+
initialization. Works only if __db__ is a string. Defaults to false.
5658

5759
*Metadata:* Logged as a native JSON object in 'meta' property.
5860

lib/winston-mongodb.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @module winston-mongodb
2+
* @module 'winston-mongodb'
33
* @fileoverview Winston transport for logging into MongoDB
44
* @license MIT
55
* @author [email protected] (Charlie Robbins)
@@ -40,6 +40,15 @@ var helpers = require('./helpers');
4040
* @param {string} options.label Label stored with entry object if defined.
4141
* @param {string} options.name Transport instance identifier. Useful if you
4242
* need to create multiple MongoDB transports.
43+
* @param {boolean=false} options.capped In case this property is true,
44+
* winston-mongodb will try to create new log collection as capped.
45+
* @param {number=10000000} options.cappedSize Size of logs capped collection
46+
* in bytes.
47+
* @param {number} options.cappedMax Size of logs capped collection in number
48+
* of documents.
49+
* @param {boolean=false} options.tryReconnect Will try to reconnect to the
50+
* database in case of fail during initialization. Works only if `db` is
51+
* a string.
4352
*/
4453
var MongoDB = exports.MongoDB = function(options) {
4554
winston.Transport.call(this, options);
@@ -127,12 +136,12 @@ var MongoDB = exports.MongoDB = function(options) {
127136
db.authenticate(self.username, self.password,
128137
function(err, result) {
129138
if (err) {
130-
console.error('winston-mongodb: error initialising logger', err);
139+
cb(err, null);
131140
db.close();
132141
return;
133142
}
134143
if (!result) {
135-
console.error('winston-mongodb: invalid username or password');
144+
cb(new Error('invalid username or password'), null);
136145
db.close();
137146
return;
138147
}
@@ -144,14 +153,22 @@ var MongoDB = exports.MongoDB = function(options) {
144153
}
145154
}
146155

147-
if ('string' === typeof this.db) {
148-
mongodb.MongoClient.connect(this.db, this.options, function(err, db) {
156+
function connectToDatabase(logger) {
157+
mongodb.MongoClient.connect(logger.db, logger.options, function(err, db) {
149158
if (err) {
150159
console.error('winston-mongodb: error initialising logger', err);
160+
if (options.tryReconnect) {
161+
console.log('winston-mongodb: will try reconnecting in 10 seconds');
162+
setTimeout(function() { connectToDatabase(logger); }, 10000);
163+
}
151164
return;
152165
}
153166
setupDatabaseAndEmptyQueue(db);
154167
});
168+
}
169+
170+
if ('string' === typeof this.db) {
171+
connectToDatabase(this);
155172
} else if ('function' === typeof this.db.then) {
156173
this.db.then(function(db) {
157174
setupDatabaseAndEmptyQueue(db);

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "winston-mongodb",
33
"license": "MIT",
4-
"version": "1.3.0",
4+
"version": "1.4.0",
55
"description": "A MongoDB transport for winston",
66
"author": "Charlie Robbins <[email protected]>",
77
"contributors": [
@@ -15,11 +15,11 @@
1515
},
1616
"keywords": ["logging", "sysadmin", "tools", "winston", "mongodb", "log", "logger"],
1717
"dependencies": {
18-
"mongodb": ">=2.0.46 <3.0.0"
18+
"mongodb": "^2.0.46"
1919
},
2020
"devDependencies": {
2121
"winston": ">=1.1.1 <3.0.0",
22-
"vows": "0.8.x",
22+
"vows": "~0.8.1",
2323
"bluebird": ">=2.10.2 <4.0.0"
2424
},
2525
"main": "./lib/winston-mongodb",

0 commit comments

Comments
 (0)