11/**
2- * @module winston-mongodb
2+ * @module ' winston-mongodb'
33 * @fileoverview Winston transport for logging into MongoDB
44 * @license MIT
55 * @author charlie@nodejitsu.com (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 */
4453var 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 ) ;
0 commit comments