Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Save Logs to mongodb Database for winston-nodejs

I am using the winston library: https://github.com/flatiron/winston Attempting to store data to the mongodb database with: https://github.com/indexzero/winston-mongodb

to insert the data I use:

var MongoDB = require('winston-mongodb').MongoDB;
var logger = new (winston.Logger)({
transports: [
    new (winston.transports.Console)(),
    new (winston.transports.MongoDB)({ host: ip,  db: 'caribcultivate', collection: 'log', level: 'info'})
], exceptionHandlers: [ new winston.transports.Console() ]
});
logger.log('info', "Running logs "+ d);
logger.info("Drive: "+ (new Date(d)).toDateString());

However when I try to query the data using:

winston.query(options, function (err, results) {
    if (err) {console.log(err);}
    console.log(results);
});

I get:

{}

It works for the Console correctly, and I am using the database in other parts of the application with the Mongoose library.

like image 853
kyleED Avatar asked Dec 18 '12 21:12

kyleED


2 Answers

I was having a similar issue. It turned out the problem for me was that the Winston MongoDB transport expects the host option to be just the host name and I was prefixing it with mongodb://.

The following works for me after removing mongodb:// from mongodb://123456.mongolab.com:

var logger = new(winston.Logger)({
    transports : [
        new(winston.transports.MongoDB)({
            db : 'logs',
            host : '123456.mongolab.com',
            username : 'username',
            password : 'password'
        })
    ]
});
like image 91
geaw35 Avatar answered Oct 11 '22 23:10

geaw35


It should be so simple, it will be all in one line:

db : 'mongodb://myuser:[email protected]:54545/MyLogDB
like image 25
securecurve Avatar answered Oct 12 '22 00:10

securecurve