Nodejs winston登录到mongodb

时间:2015-03-20 16:32:24

标签: node.js mongodb

我尝试为我的项目进行高级日志记录。

var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;
var logger = new(winston.Logger)({
    transports : [
        new(winston.transports.MongoDB)({
            db : 'logs'
        })
    ]
});
module.exports = logger;

我收到了一个错误:

/opt/City13/node_modules/winston-mongodb/node_modules/mongodb/lib/url_parser.js:16
throw Error("URL must be in the format mongodb://user:pass@host:port/dbnam
      ^
Error: URL must be in the format mongodb://user:pass@host:port/dbname
at Error (<anonymous>)
at module.exports (/opt/City13/node_modules/winston-mongodb/node_modules/mongodb/lib/url_parser.js:16:11)
at Function.MongoClient.connect (/opt/City13/node_modules/winston-mongodb/node_modules/mongodb/lib/mongo_client.js:95:16)
at new exports.MongoDB (/opt/City13/node_modules/winston-mongodb/lib/winston-mongodb.js:124:25)
at Object.<anonymous> (/opt/City13/logger/index.js:5:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)

我尝试将主机IP地址添加到设置中,但它无法正常工作。

2 个答案:

答案 0 :(得分:4)

来自他们的文档:db: MongoDB connection uri or preconnected db object.

你给了它一个字符串'logs'。它想要的是连接所需的整个mongodb网址。

他们在错误消息中给出了一个示例:mongodb://user:pass@host:port/dbname

如果你想指定集合(大概是你试图用'logs'做什么),还有一个叫做'collection'的选项

答案 1 :(得分:4)

这个配置对我有用:

    var logger = new (winston.Logger)({
    transports: [
        new(winston.transports.MongoDB)({
            db : 'mongodb://localhost:27017/Book-catalog',
            collection: 'logs'
        })
       ]
    });

(&#39; Book-catalog&#39;是我的数据库的名称)