How to configure the Log level for the mongocxx?
I have provided custom logger implementation to instance. However, I could not find any way to configure the log level. Looking forward to some help
Hi @Milind_T ,
You should be able to set the log level with log_level
enum - mongo-cxx-driver/logger.hpp at master · mongodb/mongo-cxx-driver · GitHub
API Doc - MongoDB C++ Driver: mongocxx::logger Class Reference
Example - mongo-cxx-driver/logging.cpp at master · mongodb/mongo-cxx-driver · GitHub
thanks for reply. Yes, I did follow this.
I could not figure out where to set this enum - log_level
Is there any global or any where we need to set “log_level” ?
I found old issue here: https://jira.mongodb.org/browse/SERVER-9966
However, this does not seem to be applicable as mongo::logLevel does not exist
Which Mongo client are you using?
If you want to set log level using database command, you can try db.setLogLevel.
Refer setLogLevel Doc - db.setLogLevel
This question is specific to C++ mongo driver.
Want to enable debug /trace logging for mongo driver (Mongocxx).
I guess, db.setLogLevel is for changing log level for database. I need it to configure log level for Mongo Driver (c++)
Hello @Milind_T
Yes . db.setLogLevel is for configuring log level for database.
What is your purpose of Mongo Driver(c++) ? If database connectivity, and then db.setLogLevel can be useful.
You can try logger.operator() . refer, doc
This does not help. It is just interface method to invoke logging from Mongo driver.
I need something to configure the log level for the mongo c++ driver.
Just wondering if anyone has used logging before in the c++ driver
You can look at this example - you’d need to pass a logger to the instance mongo-cxx-driver/connect.cpp at master · mongodb/mongo-cxx-driver · GitHub
@Milind_T, just posting this here as I found the example from @Rishabh_Bisht helpful but it can be useful to see something here you can just copy/paste:
#include <iostream>
#include <cstdint>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/logger.hpp>
class logger final : public mongocxx::logger {
public:
explicit logger(std::ostream* stream) : _stream(stream) {}
void operator()(mongocxx::log_level level,
bsoncxx::stdx::string_view domain,
bsoncxx::stdx::string_view message) noexcept override {
if (level >= mongocxx::log_level::k_trace)
return;
*_stream << '[' << mongocxx::to_string(level) << '@' << domain << "] " << message << '\n';
}
private:
std::ostream* const _stream;
};
int main(int, char**) {
mongocxx::instance inst{bsoncxx::stdx::make_unique<logger>(&std::cout)};
mongocxx::uri uri("mongodb+srv://...");
std::cout << "Connecting to MongoDB Atlas ...\n";
mongocxx::client conn{uri};
// ...
}
thank you! this is helpful