Question

I am trying to set up logging for the native mongo driver for node. I've got the following snippet set up as a demonstration for what I am trying to do. Unfortunately nothing is being emitted on the console. Any ideas?

var express = require('express')   ;
var app = express();
var http = require('http');

var mongod = require('mongodb');

var server_conf = new mongod.Server('localhost', 27017, {auto_reconnect:true});

//dummy logger
var logger =  {
    error:function(message, object) {console.log('anything')},
    log:function(message, object) {console.log('anything')},
    debug:function(message, object) {console.log('anything')}}


var db_container = {db: new mongod.Db('test', server_conf,
    {w:1,journal:true, native_parser:true, logger: logger})}

app.use(express.bodyParser());
app.use(app.router);

db_container.db.open(function(err, index_info){

    if(err) throw err;
    var testcol = db_container.db.collection('testcol');

    app.get('/', function(request, res){

        testcol.insert({hello:"moto"}, function(err,doc){

            if(err){
                throw err;
            }
            testcol.find({}).toArray(function(err,docs){
                res.send(docs);
            });
        });

    });

    http.createServer(app).listen(3000, function () {
        console.log('Express server listening on port ' + '3000');
    });
});
Was it helpful?

Solution

Replicated below is: a copy of the answer to this question I posted on the node-mongodb-native google group

After looking at the most recent version of the code (commit 0fd78b3278da8deac980cb876fcb2bf72eaafb79) it looks like logging isn't really implemented yet.

First some ground rules: If you create a server config object (http://mongodb.github.io/node-mongodb-native/api-generated/server.html?highlight=server#Server) and set the logger object there that's ok. If you create a Db() object and set a logger in its options then it will override any logger you have set its server config. If you create a MongoClient() object this is equivalent to new Db('test', serverOptions, options) so you can set your logger object in either serverOptions or options. I'm not going to cover MongoClient.Connect and what happens there because I don't care to look over that portion of the code.

I could only find usage of the logger object happening in connection.js where it's revealed that you actually need a few additional properties to get logging working. i.e line 69

if(this.logger != null && this.logger.doDebug){
      this.logger.debug("opened connection", this.socketOptions);
} 

or line 307

if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);

There are many more doDebug/doError property lookups in connection.js that require logging to work. There is a very minimal amount of logging happening from the looks of it but if you want to enable it you need to set the doError/doLog/doDebug properties on your logger as well. I actually haven't tested this out since I don't have the proper setup here but from looking over the code this seems to be the case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top