Overview
In this guide, you can learn how to use the C++ driver to configure logging for your application. Logging allows you to view a discrete, event-based log of driver activities.
Loggers log messages at a severity, or verbosity, level that you can specify. The driver categorizes log messages at the following levels, listed from highest to lowest severity:
errorcriticalwarningmessageinfodebugtrace
When you specify a severity level, the driver also logs all
messages with higher severity levels. For example, if you set the log level to
warning, the driver will also log messages with error and critical severity
levels.
The lower the severity level you specify, the more information the driver logs, which might impact the performance of your application.
Create a Filtered Logger
You can create a user defined logger implementation to handle logger output.
To configure your application to receive messages about driver events, define an
implementation of the logger() class.
The following example performs the following actions to create a logger that
filters log messages at the INFO severity level:
Defines a filtered logger class to handle log messages at a certain level and above.
Creates a new filtered logger for the client with a logging level of
infoand above.Tests the logger with an insert operation.
// Creates a filtered logger that only logs messages at or above a specified level class filtered_logger : public mongocxx::logger { public: // Sets the default minimum log level to WARNING explicit filtered_logger(mongocxx::log_level min_level = mongocxx::log_level::k_warning) : min_level_(min_level) {} void operator()(mongocxx::log_level level, std::string_view domain, std::string_view message) noexcept override { // Only log messages at or above the minimum level if (level <= min_level_) { std::cout << "[" << level_to_string(level) << "] " << domain << ": " << message << std::endl; } } private: mongocxx::log_level min_level_; std::string level_to_string(mongocxx::log_level level) const { switch (level) { case mongocxx::log_level::k_error: return "ERROR"; case mongocxx::log_level::k_critical: return "CRITICAL"; case mongocxx::log_level::k_warning: return "WARNING"; case mongocxx::log_level::k_message: return "MESSAGE"; case mongocxx::log_level::k_info: return "INFO"; case mongocxx::log_level::k_debug: return "DEBUG"; case mongocxx::log_level::k_trace: return "TRACE"; default: return "UNKNOWN"; } } }; // Creates a new logger for the client void example_filtered_logger() { std::cout << "\n=== Filtered Logger (INFO and above) ===" << std::endl; // Logs INFO, MESSAGE, WARNING, CRITICAL, and ERROR messages auto logger = std::make_unique<filtered_logger>(mongocxx::log_level::k_info); mongocxx::instance instance{std::move(logger)}; mongocxx::uri uri("mongodb://127.0.0.1:27017"); mongocxx::client client(uri); auto collection = client["test"]["example3"]; bsoncxx::builder::stream::document doc{}; doc << "name" << "Filtered Test"; collection.insert_one(doc.view()); std::cout << "Operation completed! (Debug and Trace messages were filtered out)" << std::endl; }
=== Filtered Logger (INFO and above) === [INFO] mongocxx: libmongoc logging callback enabled Operation completed! (Debug and Trace messages were filtered out)
The logger output contains the time of the operation, the severity level of the message, and the log message.
API Documentation
For more information about logger options for the C++ driver, see the following API documentation: