【问题标题】:Logging with winston-mongodb and express-winston使用 winston-mongodb 和 express-winston 进行日志记录
【发布时间】:2018-06-20 12:39:32
【问题描述】:

我正在尝试使用 express-winston 和 winston-mongodb 将请求/响应记录到 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);

最后,我面临两个问题;

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

  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-mongodbmeteor 一起使用时遇到了同样的问题——所有内容都存储为null。但这一切都以正确的级别和元数据正确打印到console - 你也一样吗?

标签: node.js mongodb express winston


【解决方案1】:

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

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

npm install winston-mongodb@3.0.0 --save

参考 github 问题 cb is not a function

【讨论】:

  • 嗨,您能否通过解决方案将其转化为正确的答案,以便有此问题的人可以解决?提前致谢!
  • 嗨,我刚刚解决了这个问题,安装winston-mongodb包,版本号为3.0.0
  • 感谢您的解决方案,它解决了我的问题。所以这只是节点模块之间的版本冲突。
  • 谢谢,只是补充一点,winston@3.3.3 与 winston-mongodb@3.0.2 配合得很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 2018-06-21
  • 2019-09-29
  • 2015-04-15
  • 1970-01-01
相关资源
最近更新 更多