Overview
在本指南中,您可以学习;了解如何使用C驾驶员为应用程序配置结构化日志记录。日志记录允许您查看基于事件的驾驶员活动的离散日志。
记录器按您可以指定的严重性或详细程度日志消息。当您在应用程序中启用时,您可以接收不同详细程度的应用程序活动信息。驾驶员允许您日志按以下严重性级别分类的信息:
emergency
alert
critical
error
warn
notice
info
debug
trace
off
前面的列表按严重性级别降序排列,其中 emergency
为最高严重性级别。指定严重性级别时,驾驶员还会记录所有具有更高严重性级别的消息。示例,如果将日志级别设立为 critical
,驾驶员还将日志严重性级别为 emergency
和 alert
的消息。
指定的严重性级别越低,驾驶员记录的信息就越多,这可能会影响应用程序的性能。
提示
要了解有关日志记录严重性级别的更多信息,请参阅有关消息日志记录的 系统日志标准的 维基百科条目。
日志记录选项
结构化日志设置由 mongoc_structured_log_opts_t
实例显式跟踪。C驾驶员从环境变量中获取默认设置,并提供其他可选的编程配置。使用 mongoc_structured_log_opts_new()
指定环境变量。要学习;了解有关 mongoc_structured_log_opts_t
的更多信息,请参阅 libmongoc API文档中的 mongoc_ Structured_log_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()
函数将日志记录选项应用于客户端或客户端端池
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文档中的结构化日志记录。