Skip to content

Instantly share code, notes, and snippets.

@dankahle
Last active August 29, 2015 14:17
Show Gist options
  • Save dankahle/06d21584a6bed63cc7cf to your computer and use it in GitHub Desktop.
Save dankahle/06d21584a6bed63cc7cf to your computer and use it in GitHub Desktop.
winston-mongodb log dropping bug
This code demonstrates a bug ( https://github.com/winstonjs/winston-mongodb/issues/48 ) in winston-mongodb where if you log in the narrow window between this.MainDb existing and the authorizeDb/createCollection calls, the database logs get dropped. Not shocking,just that the logs need to be queued till "after" those to calls is all.
Sorry for the confusing code, but this is why I was bitching about the lib not taking database promises, this could would be terribly simple with that 6 line change, and all the hard work's been done anyway (the queueing of logs).
Anyway, the main file (loggerTest.js) will depend on 2 async modules (dbConn and logger) with logger depending on dbconn. The test.js module will log the second it gets a chance to, but... it won't get called until the init work is done. All works as expected, but there's a bug in winston-mongodb as it tries to authorize the database and create the collection while allowing logging to go on. Something fails in that and not even worth looking into as clearly you don't want to be doing that while logging.
var mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient,
Q = require('q'),
def = Q.defer(),
db,
initialized
module.exports = function(init) {
if(init && db)
return Q(db)
else if(init && initialized)
return def.promise;
else if(init ) {
initialized = true;
MongoClient.connect('mongodb://localhost:27017/db', function (err, _db) {
if (err) return def.reject(err);
db = _db;
def.resolve(_db)
});
return def.promise;
}
else
return db;
}
/*
var mongoose = require('mongoose'),
Q = require('q'),
def = Q.defer()
var db = mongoose.createConnection('localhost', 'db');
db.p_mongo = def.promise;
db.on('error', function(err) {
db.p_mongo.reject(err);
throw err;
})
db.on('open', function(a,b) {
def.resolve(db)
})
module.exports = db;*/
var winston = require('winston'),
p_db = require('./dbconn')('init'),
Q = require('q'),
def = Q.defer(),
logger,
initialized
require('winston-mongodb').MongoDB // adds the transport to winston automatically
require('winston-mail').Mail
module.exports = function(init) {
if(init && logger)
return Q(logger)
else if(init && initialized)
return def.promise;
else if(init) {
p_db.then(function(db) {
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({level: 'error'}),
new (winston.transports.MongoDB)({
level: 'info',
//db: 'mongodb://localhost:27017/db',
db: db,
collection: 'prodlog',
exitOnError: false
})
],
exceptionHandlers: [
new (winston.transports.File)({
filename: 'logs/exceptions.log',
exitOnError: false
})
]
});
def.resolve(logger)
}, function(err) {
def.reject(err)
})
return def.promise;
}
else
return logger;
}
var Q = require('q'),
p_logger = require('./logger')('init'),
p_db = require('./dbconn')('init')
Q.all([p_logger, p_db]).then(function(results) {
test = require('./test')
}, function(err) {
throw err;
})
var logger = require('./logger')(),
db = require('./dbconn')()
logger.silly('hey')
logger.debug('hey')
logger.verbose('hey')
logger.info("127.0.0.1 - there's no place like home");
logger.warn("127.0.0.1 - there's no place like home");
logger.error("127.0.0.1 - there's no place like home");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment