10

I'm trying to log request/response into MongoDB within NodeJS project using express-winston and winston-mongodb. Here is an example code that I worked so far;

const expressWinston = require('express-winston');
const winston = require('winston'); 
require('winston-mongodb').MongoDB;

const logger = expressWinston.logger({
 transports: [
    winston.add(winston.transports.MongoDB, {
        db : 'something',
        collection : 'something',
        level : 'info',
        capped : true
    })
 ]
});

I'm exporting this logger and using it my index.js;

app.use(logger);

And at the end, I'm facing 2 problems;

  1. A new entry is created in my Mongo collection for each request/response but they are empty as shown below enter image description here

  2. I got an exception even the entry is created;

    TypeError: cb is not a function at logDb.collection.insertOne.then.catch.err (\node_modules\winston-mongodb\lib\winston-mongodb.js:213:7)

Here is the code block from winston-mongodb.js that causes the exception;

this.logDb.collection(this.collection).insertOne(entry).then(()=>{
  console.error('55dddddrrr', {});
  this.emit('logged');
  **cb(null, true);**
})

I've been trying to solve this but couldn't came up with anything useful yet. Would appreciate any help on the issue.

josh
  • 409
  • 1
  • 5
  • 18
  • I have the same issue trying to use `winston-mongodb` with `meteor` - everything is stored as `null`. But it's all printing correctly to the `console` with the right level and meta - is that the same for you? – rubie Jan 12 '18 at 10:44

1 Answers1

8

I have the same issue, seems that the winston-mongodb log function takes different argument ( info as the meta object to be logged into the mongo database, cb is the callback function if you want to see the result after the log operation on the mongo done)

Solution : install the winston-mongodb package with the version no 3.0.0

npm install winston-mongodb@3.0.0 --save

reference to the github issue cb is not a function

M.elsayed
  • 96
  • 6
  • HI, would you be able to turn this into a proper answer with a solution so that people with this problem can solve it? Thanks in advance! – rubie Jan 13 '18 at 09:32
  • 1
    Hi ,i just solved this issue , install the winston-mongodb package with the version no 3.0.0 – M.elsayed Jan 14 '18 at 11:02
  • Thanks for the solution, it solved my issue. So it was just a version conflict between node modules. – josh Jan 15 '18 at 06:40
  • Thanks, just to add that winston@3.3.3 works well with winston-mongodb@3.0.2 – Cuado Dec 18 '20 at 16:19