Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C 드라이버

구조화된 로깅

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

로거는 사용자가 지정할 수 있는 심각도 또는 상세도 수준으로 메시지를 로그 . 애플리케이션 로그인을 활성화 하면 다양한 수준에서 애플리케이션 활동에 대한 정보를 받을 수 있습니다. 운전자 사용하면 다음과 같은 심각도 수준으로 분류된 정보를 로그 할 수 있습니다.

  • emergency

  • alert

  • critical

  • error

  • warn

  • notice

  • info

  • debug

  • trace

  • off

앞의 목록은 심각도 수준이 낮은 순으로 정렬되며 emergency 이 가장 높은 심각도 수준입니다. 심각도 수준을 지정하면 운전자 심각도 수준이 더 높은 모든 메시지도 기록합니다. 예시 들어 로그 수준을 critical로 설정하다 운전자 emergencyalert 심각도 수준의 메시지도 로그 합니다.

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

로깅 심각도 수준에 대해 자세히 알아보려면 메시지 로깅에 대한 Syslog 표준에 대한 Wikipedia 항목을 참조하세요.

구조화된 로그 설정은 mongoc_structured_log_opts_t 인스턴스 에서 명시적으로 추적합니다. C 운전자 환경 변수에서 기본값 설정을 가져오고 추가 선택적 프로그래밍 구성을 제공합니다. mongoc_structured_log_opts_new() 를 사용하여 환경 변수를 지정합니다. mongoc_structured_log_opts_t에 대해 자세히 학습하려면 libmongoc API 문서에서 mongoc_구조화된_로그_opts_t 를 참조하세요.

일반적으로 환경 변수는 프로그래밍 방식으로 재정의할 수 있는 기본값을 제공합니다. 환경에서 프로그래밍 방식 기본값을 재정의할 수 있는 반대 동작을 요청 하려면 mongoc_structured_log_opts_set_max_levels_from_env() 함수를 사용합니다. 구조화된 로그 메시지는 핸들러에서 임의의 방식으로 필터링할 수 있지만, 성능 최적화와 편의를 위해 내장 필터하다 구성 요소별 설정으로 보고된 메시지의 최대 로그 수준을 제한합니다. mongoc_structured_log_opts_set_max_levels_from_env()에 대해 자세히 학습하려면 libmongoc API 문서에서 mongoc_structured_log_opts_set_max_levels_from_env() 를 참조하세요.

다음 코드 차단 다음을 수행하여 로깅을 구현합니다.

  • 로깅을 구성하기 위해 mongoc_structured_log_opts_t 유형을 생성합니다.

  • 원하는 로그 수준을 다음으로 설정합니다. INFO

  • 로그 메시지를 프로세스 하는 핸들러 함수 제공

  • mongoc_client_set_structured_log_opts() 함수를 사용하여 클라이언트 또는 클라이언트 풀에 로깅 옵션을 적용합니다.

#include <mongoc/mongoc.h>
#include <stdio.h>
static void log_handler(const mongoc_structured_log_entry_t *entry, void *user_data) {
printf("Log: %s - %s\n",
mongoc_structured_log_get_component_name(mongoc_structured_log_entry_get_component(entry)),
mongoc_structured_log_entry_get_message_string(entry));
}
int main(void) {
mongoc_init();
// Create logging options
mongoc_structured_log_opts_t *log_opts = mongoc_structured_log_opts_new();
mongoc_structured_log_opts_set_max_level_for_all_components(log_opts, MONGOC_STRUCTURED_LOG_LEVEL_INFO);
mongoc_structured_log_opts_set_handler(log_opts, log_handler, NULL);
// Create client and set logging
mongoc_client_t *client = mongoc_client_new("<connection-string>");
mongoc_client_set_structured_log_opts(client, log_opts);
// Perform operation to generate logs
bson_t *command = BCON_NEW("ping", BCON_INT32(1));
bson_t reply;
bson_error_t error;
mongoc_client_command_simple(client, "admin", command, NULL, &reply, &error);
// Cleanup
bson_destroy(command);
bson_destroy(&reply);
mongoc_structured_log_opts_destroy(log_opts);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}

debug 수준에서 애플리케이션을 실행하고 연결에 대해 기록하는 경우 드라이버는 연결을 열고, 사용하고, 닫을 때마다 메시지를 전송합니다. 다음 코드는 이 로깅 사양에 대한 명령을 보여줍니다.

mongoc_structured_log_opts_t *log_opts = mongoc_structured_log_opts_new();
mongoc_structured_log_opts_set_max_level_for_all_components(
log_opts, MONGOC_STRUCTURED_LOG_LEVEL_DEBUG);
mongoc_structured_log_opts_set_handler(log_opts, debug_log_handler, NULL);
// Create client with DEBUG logging enabled
mongoc_client_t *client = mongoc_client_new("<connection-string>");
mongoc_client_set_structured_log_opts(client, log_opts);

debug 수준 추적이 지정된 상태에서 연결을 열고 사용할 때 운전자 debug 로그 메시지를 생성합니다.

bson_t *doc = BCON_NEW("x", BCON_INT32(1));
bson_error_t error;
if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) {
fprintf(stderr, "Insert failed: %s\n", error.message);
}
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection pool created topologyId="..." serverHost="..." serverPort=27017
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection pool created topologyId="..." serverHost="..." serverPort=27017
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection pool created topologyId="..." serverHost="..." serverPort=27017
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection pool ready topologyId="..." serverHost="..." serverPort=27017
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection checkout started topologyId="..." serverHost="..." serverPort=27017
[2023-07-21T18:13:00Z DEBUG mongodb::connection] Connection created topologyId="..." serverHost="..." serverPort=27017 driverConnectionId=1
...

C 운전자 에서 다른 로그 수준을 지정하는 방법에 대해 자세히 학습 libmongoc API 문서의 수준 및 구성 요소 를 참조하세요.

클라이언트 옵션 설정에 대해 자세히 학습 MongoDB 에 연결 가이드 참조하세요. C 운전자의 구조화된 로깅에 대해 자세히 학습하려면 libmongoc API 문서에서 구조화된 로깅 을 참조하세요.

모니터링

로깅 외에도 애플리케이션 에서 모니터링 활성화 할 수 있습니다. 자세한 학습 은 클러스터 모니터링 가이드를 참조하세요.

돌아가기

엔터프라이즈 인증

이 페이지의 내용