问题描述
我正在尝试使用express-winston和winston-mongodb将请求/响应记录到NodeJS项目中的MongoDB中.这是到目前为止我工作过的示例代码;
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
})
]
});
我正在导出此记录器,并在index.js中使用它;
I'm exporting this logger and using it my index.js;
app.use(logger);
最后,我面临两个问题;
And at the end, I'm facing 2 problems;
-
在我的Mongo集合中为每个请求/响应创建了一个新条目,但它们为空,如下所示
A new entry is created in my Mongo collection for each request/response but they are empty as shown below
即使创建了条目,我也遇到了异常;
I got an exception even the entry is created;
TypeError:cb不是函数在logDb.collection.insertOne.then.catch.err(\ node_modules \ winston-mongodb \ lib \ winston-mongodb.js:213:7)
TypeError: cb is not a functionat logDb.collection.insertOne.then.catch.err (\node_modules\winston-mongodb\lib\winston-mongodb.js:213:7)
这是winston-mongodb.js中导致异常的代码块;
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.
推荐答案
我遇到了同样的问题,似乎winston-mongodb日志函数采用了不同的参数(info作为要登录到mongo数据库的元对象,cb是如果想要在mongo上执行日志操作后查看结果,则返回回调函数)
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)
解决方案:安装版本号为3.0.0的Winston-mongodb软件包
Solution :install the winston-mongodb package with the version no 3.0.0
npm install [email protected] --save
npm install [email protected] --save
对github问题的引用 cb不是函数
reference to the github issue cb is not a function
这篇关于使用Winston-MongoDB和Express-Winston进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!