1
1
/**
2
- * @module winston-mongodb
2
+ * @module ' winston-mongodb'
3
3
* @fileoverview Winston transport for logging into MongoDB
4
4
* @license MIT
5
5
* @author [email protected] (Charlie Robbins)
@@ -40,6 +40,15 @@ var helpers = require('./helpers');
40
40
* @param {string } options.label Label stored with entry object if defined.
41
41
* @param {string } options.name Transport instance identifier. Useful if you
42
42
* 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.
43
52
*/
44
53
var MongoDB = exports . MongoDB = function ( options ) {
45
54
winston . Transport . call ( this , options ) ;
@@ -127,12 +136,12 @@ var MongoDB = exports.MongoDB = function(options) {
127
136
db . authenticate ( self . username , self . password ,
128
137
function ( err , result ) {
129
138
if ( err ) {
130
- console . error ( 'winston-mongodb: error initialising logger' , err ) ;
139
+ cb ( err , null ) ;
131
140
db . close ( ) ;
132
141
return ;
133
142
}
134
143
if ( ! result ) {
135
- console . error ( 'winston-mongodb: invalid username or password') ;
144
+ cb ( new Error ( ' invalid username or password') , null ) ;
136
145
db . close ( ) ;
137
146
return ;
138
147
}
@@ -144,14 +153,22 @@ var MongoDB = exports.MongoDB = function(options) {
144
153
}
145
154
}
146
155
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 ) {
149
158
if ( err ) {
150
159
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
+ }
151
164
return ;
152
165
}
153
166
setupDatabaseAndEmptyQueue ( db ) ;
154
167
} ) ;
168
+ }
169
+
170
+ if ( 'string' === typeof this . db ) {
171
+ connectToDatabase ( this ) ;
155
172
} else if ( 'function' === typeof this . db . then ) {
156
173
this . db . then ( function ( db ) {
157
174
setupDatabaseAndEmptyQueue ( db ) ;
0 commit comments