winston-mongodb日志连接失败

时间:2014-12-17 02:54:17

标签: mongodb express mongoose mlab nodejitsu

我的快速网络应用程序使用Winston进行日志记录,并使用winston-mongodb将日志保存到mongolab托管的mongoDB(副本集)。

一切都运行良好几天,然后当流量有所增加时,日志就停止了保存/连接到数据库。不幸的是,这意味着我没有日志来检查出现了什么问题 - 令人沮丧的情况。其他所有产品仍然正常工作 - 其他产品系列(如用户)正在保存和更新,服务器正常运行。

我尝试重新部署/重新启动服务器无济于事,日志不会再次开始录制。我怀疑我的服务器/记录器设置有一些细微的问题,可能是与数据库连接有关的竞争条件,但我现在真的输了。这是代码:

//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; 

对于导致日志不再起作用的任何想法都将非常感激。

1 个答案:

答案 0 :(得分:1)

当Winston建立与Mongo实例的连接时,它专门查找replicaSet标志以确定连接是否是副本集。否则,它将连接到dbUri中的第一个主机。 (来源https://github.com/indexzero/winston-mongodb/blob/master/lib/winston-mongodb.js#L28

要为Winston配置一个能够正确处理故障转移的连接,请使用以下URI:

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

如果您想测试代码以验证它是否可以正常处理副本集故障转移,您可以使用此处描述的名为触发器的测试集群:http://mongolab.org/flip-flop/

(完全披露:我为MongoLab工作。)