Skip to content

Commit

Permalink
fixed transport.close() call throwing an exception; closes #130
Browse files Browse the repository at this point in the history
  • Loading branch information
yurijmikhalevich committed Mar 13, 2019
1 parent 5454d1d commit 536a622
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ new log collection as capped, defaults to false.
initialization. Works only if __db__ is a string. Defaults to false.
* __decolorize:__ Will remove color attributes from the log entry message,
defaults to false.
* __leaveConnectionOpen:__ Will leave MongoClient connected after transport shut down.
* __expireAfterSeconds:__ Seconds before the entry is removed. Works only if __capped__ is not set.

*Metadata:* Logged as a native JSON object in 'meta' property.
Expand Down
29 changes: 22 additions & 7 deletions lib/winston-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const helpers = require('./helpers');
* a string.
* @param {boolean=false} options.decolorize Will remove color attributes from
* the log entry message.
* @param {number} options.expireAfterSeconds Seconds before the entry is removed. Do not use if capped is set.
* @param {boolean=false} options.leaveConnectionOpen Will leave MongoClient connected
* after transport shut down.
* @param {number} options.expireAfterSeconds Seconds before the entry is removed.
* Do not use if capped is set.
*/
let MongoDB = exports.MongoDB = function(options) {
winston.Transport.call(this, options);
Expand All @@ -72,6 +75,7 @@ let MongoDB = exports.MongoDB = function(options) {
this.cappedSize = (options.cappedSize || 10000000);
this.cappedMax = options.cappedMax;
this.decolorize = options.decolorize;
this.leaveConnectionOpen = options.leaveConnectionOpen;
this.expireAfterSeconds = !this.capped && options.expireAfterSeconds;
if (this.storeHost) {
this.hostname = os.hostname();
Expand All @@ -84,7 +88,9 @@ let MongoDB = exports.MongoDB = function(options) {
self.logDb = db;
processOpQueue();
}, err=>{
db.close();
if (self.mongoClient && !self.leaveConnectionOpen) {
self.mongoClient.close();
}
console.error('winston-mongodb, initialization error: ', err);
});
}
Expand Down Expand Up @@ -120,7 +126,10 @@ let MongoDB = exports.MongoDB = function(options) {
}
function connectToDatabase(logger) {
return mongodb.MongoClient.connect(logger.db, logger.options
).then(client=>setupDatabaseAndEmptyQueue(client.db()), err=>{
).then(client=>{
logger.mongoClient = client;
setupDatabaseAndEmptyQueue(client.db());
}, err=>{
console.error('winston-mongodb: error initialising logger', err);
if (options.tryReconnect) {
console.log('winston-mongodb: will try reconnecting in 10 seconds');
Expand All @@ -133,11 +142,16 @@ let MongoDB = exports.MongoDB = function(options) {
if ('string' === typeof this.db) {
connectToDatabase(this);
} else if ('function' === typeof this.db.then) {
this.db.then(client=>setupDatabaseAndEmptyQueue(client.db()), err=>console.error(
'winston-mongodb: error initialising logger from promise', err));
this.db.then(client=>{
this.mongoClient = client;
setupDatabaseAndEmptyQueue(client.db());
}, err=>console.error('winston-mongodb: error initialising logger from promise', err));
} else if ('function' === typeof this.db.db) {
this.mongoClient = this.db;
setupDatabaseAndEmptyQueue(this.db.db())
} else { // preconnected object
console.warn(
'winston-mongodb: preconnected object support is deprecated and will be removed in v5');
setupDatabaseAndEmptyQueue(this.db);
}
};
Expand All @@ -161,10 +175,11 @@ winston.transports.MongoDB = MongoDB;
* Used by winston Logger.close on transports.
*/
MongoDB.prototype.close = function() {
if (!this.logDb) {
this.logDb = null;
if (!this.mongoClient || this.leaveConnectionOpen) {
return;
}
this.logDb.close().then(()=>this.logDb = null).catch(err=>{
this.mongoClient.close().then(()=>this.mongoClient = null).catch(err=>{
console.error('Winston MongoDB transport encountered on error during '
+ 'closing.', err);
});
Expand Down
9 changes: 9 additions & 0 deletions test/winston-mongodb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const dbUrl = process.env.USER_WINSTON_MONGODB_URL

mongoose.connect(dbUrl);

describe('winston-mongodb-manual-tests', function() {
describe('winston-mongodb', function() {
let transport = new MongoDB({db: dbUrl});
it('should be closeable', function() {
transport.close();
});
});
});

test_suite({name: '{db: url}', Transport: MongoDB, construct: {db: dbUrl}});
test_suite({name: '{db: url} on capped collection', Transport: MongoDB,
construct: {db: dbUrl, capped: true, collection: 'cappedLog'}});
Expand Down

0 comments on commit 536a622

Please sign in to comment.