Logging with winston-mongodb and express-winston

时间:2018-01-11 08:36:13

标签: node.js mongodb express winston

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.

1 个答案:

答案 0 :(得分:6)

我有同样的问题,似乎winston-mongodb日志函数采用不同的参数(info作为要登录到mongo数据库的元对象,cb是回调函数,如果你想在日志操作后看到结果在mongo完成)

解决方案: 安装版本号为3.0.0的winston-mongodb软件包

npm install winston-mongodb@3.0.0 --save

引用github问题 cb is not a function