Skip to content

Commit

Permalink
Version 1.4.0: added tryReconnect option
Browse files Browse the repository at this point in the history
  • Loading branch information
yurijmikhalevich committed Mar 18, 2016
1 parent a98daa6 commit 1f8a994
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -53,6 +53,8 @@ MongoDB transports.
new log collection as capped, defaults to false.
* __cappedSize:__ Size of logs capped collection in bytes, defaults to 10000000.
* __cappedMax:__ Size of logs capped collection in number of documents.
* __tryReconnect:__ Will try to reconnect to the database in case of fail during
initialization. Works only if __db__ is a string. Defaults to false.

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

27 changes: 22 additions & 5 deletions lib/winston-mongodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @module winston-mongodb
* @module 'winston-mongodb'
* @fileoverview Winston transport for logging into MongoDB
* @license MIT
* @author charlie@nodejitsu.com (Charlie Robbins)
@@ -40,6 +40,15 @@ var helpers = require('./helpers');
* @param {string} options.label Label stored with entry object if defined.
* @param {string} options.name Transport instance identifier. Useful if you
* need to create multiple MongoDB transports.
* @param {boolean=false} options.capped In case this property is true,
* winston-mongodb will try to create new log collection as capped.
* @param {number=10000000} options.cappedSize Size of logs capped collection
* in bytes.
* @param {number} options.cappedMax Size of logs capped collection in number
* of documents.
* @param {boolean=false} options.tryReconnect Will try to reconnect to the
* database in case of fail during initialization. Works only if `db` is
* a string.
*/
var MongoDB = exports.MongoDB = function(options) {
winston.Transport.call(this, options);
@@ -127,12 +136,12 @@ var MongoDB = exports.MongoDB = function(options) {
db.authenticate(self.username, self.password,
function(err, result) {
if (err) {
console.error('winston-mongodb: error initialising logger', err);
cb(err, null);
db.close();
return;
}
if (!result) {
console.error('winston-mongodb: invalid username or password');
cb(new Error('invalid username or password'), null);
db.close();
return;
}
@@ -144,14 +153,22 @@ var MongoDB = exports.MongoDB = function(options) {
}
}

if ('string' === typeof this.db) {
mongodb.MongoClient.connect(this.db, this.options, function(err, db) {
function connectToDatabase(logger) {
mongodb.MongoClient.connect(logger.db, logger.options, function(err, db) {
if (err) {
console.error('winston-mongodb: error initialising logger', err);
if (options.tryReconnect) {
console.log('winston-mongodb: will try reconnecting in 10 seconds');
setTimeout(function() { connectToDatabase(logger); }, 10000);
}
return;
}
setupDatabaseAndEmptyQueue(db);
});
}

if ('string' === typeof this.db) {
connectToDatabase(this);
} else if ('function' === typeof this.db.then) {
this.db.then(function(db) {
setupDatabaseAndEmptyQueue(db);
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "winston-mongodb",
"license": "MIT",
"version": "1.3.0",
"version": "1.4.0",
"description": "A MongoDB transport for winston",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
@@ -15,11 +15,11 @@
},
"keywords": ["logging", "sysadmin", "tools", "winston", "mongodb", "log", "logger"],
"dependencies": {
"mongodb": ">=2.0.46 <3.0.0"
"mongodb": "^2.0.46"
},
"devDependencies": {
"winston": ">=1.1.1 <3.0.0",
"vows": "0.8.x",
"vows": "~0.8.1",
"bluebird": ">=2.10.2 <4.0.0"
},
"main": "./lib/winston-mongodb",

0 comments on commit 1f8a994

Please sign in to comment.