Search code examples
mongodbexpressmongoosemlabnodejitsu

winston-mongodb log connection fails


My express web app uses Winston for logging, and the logs are saved to a mongolab hosted mongoDB (replica set) using winston-mongodb.

Everything was working fine for a few days, then when traffic picked up a bit the logs just stopped saving/connecting to the DB. Unfortunately this means I have no logs to check to see what went wrong - a frustrating situation. Everything else is still working fine - other collections (like users) are saving and updating correctly, the server is up.

I've tried redeploying/restarting the server to no avail, the logs won't start recording again. I suspect there is some nuanced problem with my server/logger setup, possibly a race condition related to the DB connection, but I'm really lost right now. Here's the code:

//server.js (simplified required modules - only ones possibly relevant to question)
var express = require( 'express' );
var session = require('express-session');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var mongoose = require( 'mongoose' );
var uriUtil = require('mongodb-uri');
var config = require('config');
var bodyParser = require('body-parser');
var MongoStore = require('connect-mongostore')(session);
var logger = require('./logs/logger.js'); 

// DATABASE CONNECT

var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, 
                replset: { socketOptions: { keepAlive: 1, connectTimeoutMS : 30000 } } }; 

var mongodbUri = "mongodb://<user>:<pw>@ds012345-a0.mongolab.com:12345,ds012345-a1.mongolab.com:12345/<db>"

//use mongo-uri to make sure url is in the correct format
var mongooseUri = uriUtil.formatMongoose(mongodbUri);               

mongoose.connect(mongooseUri, options);

var conn = mongoose.connection;

// __Create server__
var app = express();

conn.on('open', function(e) {

    var sessionStore = new MongoStore({ mongooseConnection: mongoose.connections[0] });

    app.use(bodyParser.json()); 
    app.use(bodyParser.urlencoded({extended: false}));  
    app.use(methodOverride()); 

    app.use(cookieParser()); 
    app.use(session({
      store: sessionStore,
      secret: config.session.secret,
      cookie: {
        maxAge: config.session.maxage,
        httpOnly: true, 
        secure: false
        //If I set 'secure:true', it prevents the session from working properly.
      },
      saveUninitialized: true,
      resave: true
    })); 

    app.use(passport.initialize());
    app.use(passport.session());

    app.use( express.static( path.join( __dirname, 'site') ) );

    //Routes
    app.use('/api/forgot', controllers.forgotpw);
    //etc.

});

conn.on('connected', function(){

//Start server

    app.listen(config.port, function() {
        logger.info( 'Express server listening on port %d in %s mode', config.port, app.settings.env );
        logger.info(process.env.NODE_ENV);
    });
});

//logger.js

var winston = require('winston');
var config = require('config');
require('winston-mongodb').MongoDB;


if (process.env.NODE_ENV == 'production') {
    var winston = new (winston.Logger)({  
        transports: [
            new (winston.transports.Console)({ level: 'warn' }),
            new (winston.transports.MongoDB)({
                //db : 'atlas_database',
                level : 'debug',
                //ssl : true,
                dbUri : "mongodb://<user>:<pw>@ds012345-a0.mongolab.com:12345,ds012345-a1.mongolab.com:12345/<db>"
            })
        ]
    });

    winston.info('Chill Winston, the logs are being captured with production settings');
}


module.exports = winston; 

Any thoughts on what is causing the logs to no longer function would be greatly appreciated.


Solution

  • When Winston establishes a connection to a Mongo instance, it looks specifically for the replicaSet flag to determine if the connection is a replica set. Otherwise, it connects to the first host in the dbUri. ( Source: https://github.com/indexzero/winston-mongodb/blob/master/lib/winston-mongodb.js#L28 )

    To have configure Winston with a connection that will properly handle failovers, use the following URI:

    dbUri : "mongodb://<user>:<pw>@ds012345-a0.mongolab.com:12345,ds012345-a1.mongolab.com:12345/<db>?replicaSet=<replsetname>"

    If you ever want to test your code to verify that it can handle a replica set failover gracefully, you can use a testing cluster called flip-flop described here: http://mongolab.org/flip-flop/

    (Full Disclosure: I work for MongoLab.)