Docs Menu
Docs Home
/ /

드라이버 이벤트 로그

이 가이드 에서는 C++ 운전자 사용하여 애플리케이션 에 대한 로깅을 구성하는 방법을 학습 수 있습니다. 로깅을 사용하면 운전자 활동에 대한 개별 이벤트 기반 로그 볼 수 있습니다.

로거는 사용자가 지정할 수 있는 심각도 또는 상세도 수준으로 메시지를 로그 . 운전자 다음 수준에서 로그 메시지를 가장 높은 심각도에서 가장 낮은 심각도 순으로 분류합니다.

  • error

  • critical

  • warning

  • message

  • info

  • debug

  • trace

심각도 수준을 지정하면 운전자 심각도 수준이 더 높은 모든 메시지도 기록합니다. 예시 를 들어 로그 수준을 warning로 설정하다 하면 운전자 errorcritical 심각도 수준의 메시지도 로그 합니다.

지정하는 심각도 수준이 낮을수록 운전자 더 많은 정보를 기록하므로 애플리케이션 성능에 영향 수 있습니다.

로거 출력을 처리하다 위해 사용자 정의 로거 구현 만들 수 있습니다.

운전자 이벤트에 대한 메시지를 수신하도록 애플리케이션 구성하려면 logger() 클래스의 구현 정의합니다.

다음 예시 다음 조치를 수행하여 INFO 심각도 수준에서 로그 메시지를 필터링하는 로거를 만듭니다.

  • 특정 수준 이상에서 로그 메시지를 처리하다 하도록 필터링된 로거 클래스를 정의합니다.

  • 로깅 수준이 info 이상인 클라이언트 에 대해 필터링된 새 로거를 만듭니다.

  • 삽입 작업으로 로거를 테스트합니다.

// 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)

로거 출력에는 작업 시간, 메시지의 심각도 및 로그 메시지가 포함됩니다.

C++ 운전자 의 로거 옵션에 대한 자세한 내용은 다음 API 설명서를 참조하세요.

  • mongocxx::logger

돌아가기

MongoDB Vector Search

이 페이지의 내용