Possible to identify a connection for logging purposes (C++)

18 views
Skip to first unread message

Saj

unread,
Oct 4, 2012, 2:43:51 PM10/4/12
to mongod...@googlegroups.com
I'm using a ScopedDbConnection from a pool. I would like to log the connection's identity to my application log so I can tie it back to mongod log.

Is there a getId() or similar method I could use (can't see one in the docs)?

Thanks,

Saj

Stephen Lee

unread,
Oct 5, 2012, 3:14:50 PM10/5/12
to mongod...@googlegroups.com
Referring to https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/Iq2vdlUEYwg, you can write something like

#include <iostream>
#include <mongo/client/dbclient.h>

using namespace mongo;

int main() {
    try {
        DBClientConnection c;
        BSONObj o;
        c.connect("mongohostname:12345");
        c.simpleCommand( "admin", &o, "whatsmyuri" );
        cout << "connected ok on " << o.getField( "you" ) << endl;
    } catch( DBException &e ) { 
        cout << "caught " << e.what() << endl;
    }   
    return 0;
}

where instead of DBClientConnection c, you'd use myScopedDbConnection.conn().  This will report the connection's URI, which you can then correlate to the mongod logs.

-Stephen
Reply all
Reply to author
Forward
0 new messages