Skip to content Skip to sidebar Skip to footer

Cappedmax Not Working In Winston-mongodb Logger In Node.js On Ubuntu

I have created a logger in Node.js using the winston module and added MongoDB transport by requiring winston-mongodb module with the following options: { db: config.db[k.DB_ENV.

Solution 1:

This is what I use to get multiple logs:

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


winston.loggers.add('userLog',{
    transports : [
        new(winston.transports.MongoDB)({
            db : 'mongodb://username:password.mongolab.com:5555/log_db',
            collection : 'userLog',
            capped : true
        }),
    ]
});
winston.loggers.add('profileLog',{
    transports : [
        new(winston.transports.MongoDB)({
            db : 'mongodb://username:password.mongolab.com:5555/log_db',                collection : 'profileLog',
            capped : true
        }),
    ]
});

And it works fine with no observable latency.

P.S. You can add all the options you want after or before capped:true

Have fun!

Solution 2:

So just to confirm that the 'capped' features of winston-mongodb which are:


capped:       //trueorfalse
cappedMax:    // [int] number of documents per collection
cappedSize:    // [byte-size] Size of documents capped per collection in bytes

ARE WORKING but it is important to note that in order for it to work you can not modify and existing collection with these properties. It is assumed that you are starting from scratch. For example if you have already created your collection and now wish to add capped features to it THEN THIS WILL NOT WORK!

You will need to completely dump the collection within the database and start from scratch or alternative just create a new collection.

For example this is an extract of code from my transports array.

newtransports.MongoDB({level:'error',db:process.env.MONGO_CONNECTION_LOG_STRING,options: { useUnifiedTopology:true },format:combine(timestamp(),json()),collection:'prod_log',capped:true,cappedMax:2})

So at present I made sure that this collection does not exists in my database, what this config will do is only allow 2 documents at anyone time to be stored in my collection, and any future errors will overwrite any existing ones so you will only every have the last two 'error' logs/documents in the collection.

Being playing around with this for sometime, I hope it helps.

Solution 3:

I had the same issue but I fixed it by simply updating the winston-mongodb version npm i winston-mongodb

Post a Comment for "Cappedmax Not Working In Winston-mongodb Logger In Node.js On Ubuntu"