使用 winston-mongodb 和 express-winston 记录

Logging with winston-mongodb and express-winston

我正在尝试使用 express-winston 和 winston-mongodb 将 request/response 登录到 NodeJS 项目中的 MongoDB。这是我到目前为止工作的示例代码;

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
    })
 ]
});

我正在导出这个记录器并在我的 index.js;

中使用它
app.use(logger);

最后,我遇到了 2 个问题;

  1. 在我的 Mongo 集合中为每个 request/response 创建了一个新条目,但它们是空的,如下所示

  2. 创建条目时出现异常;

    TypeError: cb 不是一个函数 在 logDb.collection.insertOne.then.catch.err (\node_modules\winston-mongodb\lib\winston-mongodb.js:213:7)

这是导致异常的来自 winston-mongodb.js 的代码块;

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

我一直在尝试解决这个问题,但还没有找到任何有用的方法。非常感谢有关此问题的任何帮助。

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

解决方案: 安装版本号为 3.0.0

的 winston-mongodb 软件包

npm install winston-mongodb@3.0.0 --save

参考 github 问题 cb is not a function