跟踪与日志
概述
本指南将介绍如何使用Rust驱动程序来配置应用程序的跟踪和日志记录。跟踪和日志记录是两种观察应用程序的框架。日志记录允许您查看离散的事件日志,而跟踪提供连续视图。
在Rust驱动程序中,跟踪和日志记录的功能没有太大差异。然而,当您启用跟踪时,驱动程序会以更结构化的格式发出消息,这使得应用程序以程序方式消费事件消息更加方便。
跟踪器和记录器可以在指定的严重性或详细程度级别记录消息。通过在您的应用程序中启用这些组件之一,您可以在不同的详细程度上接收有关应用程序活动的信息。
提示
要了解更多关于日志严重性的信息,请参阅维基百科上的系统日志消息记录标准.
启用跟踪和日志记录
驱动程序实现了跟踪存储库,以使驱动程序能够发出驱动事件的消息。
重要
跟踪库不稳定
因为tracing
库没有发布 1.0 版本,您可以认为其功能是不稳定的。
要启用跟踪、日志记录或两者兼有,您必须将 tracing
依赖项和 tracing-unstable
功能标志添加到您的 Cargo.toml
文件中的 mongodb
依赖项。
[dependencies] tracing = "0.1.37" [dependencies.mongodb] version = "3.1.0" features = ["tracing-unstable"]
以下表格描述了您可以对其发出事件的组件及其对应的目标
组件 | 目标 | 描述 |
---|---|---|
命令 | mongodb::command | 事件描述发送到数据库的命令以及它们是否成功或失败。 |
服务器选择 | mongodb::server_selection | 事件描述驱动器在 MongoDB 部署中选择服务器的过程。 |
连接 | mongodb::connection | 事件描述驱动器连接池的行为以及它们所包含的连接。 |
要指定日志记录组件和严重级别,您可以在编译和运行应用程序时设置 RUST_LOG
环境变量。通过将 RUST_LOG
的值设置为前面表格中提供的目标之一并包括一个严重级别来指定日志记录组件。
以下代码演示了一个在 debug
级别记录连接事件的命令
$ RUST_LOG='mongodb::connection=debug' cargo run
实现跟踪
要启用跟踪,您还必须在 Cargo.toml
文件中添加 tracing-subscriber
依赖项。以下代码演示了包含驱动器依赖项和 tracing-subscriber
库的示例依赖项列表
[dependencies] tracing = "0.1.37" tracing-subscriber = "0.3.17" [dependencies.mongodb] version = "3.1.0" features = ["tracing-unstable"]
然后,在您的应用程序中,您必须注册一个实现了 tracing::Subscriber
特性的类型以使用跟踪消费事件。以下代码演示了如何注册一个使用 RUST_LOG
环境变量指定的规范的跟踪订阅者
tracing_subscriber::fmt::init();
提示
要了解有关注册订阅者的更多信息,请参阅 跟踪
文档。
如果在 调试
级别对命令进行跟踪,则驱动程序在执行操作时发出消息。以下代码显示了此跟踪规范的命令
$ RUST_LOG='mongodb::command=debug' cargo run
指定了 调试
级别跟踪时,当你执行写操作时,驱动程序生成跟踪消息
let my_coll = client.database("db").collection("test_coll"); my_coll.insert_one(doc! { "x" : 1 }).await?;
2023-07-21T17:37:13.587794Z DEBUG mongodb::command: Command started topologyId="..." command="{\"insert\":\"test_coll\", ...}" databaseName="test" commandName="insert" requestId=12 driverConnectionId=1 serverConnectionId=133839 serverHost="..." serverPort=27017 2023-07-21T17:37:13.630401Z DEBUG mongodb::command: Command succeeded topologyId="..." reply="{\"n\":1, ...}" commandName="insert" requestId=12 driverConnectionId=1 serverConnectionId=133839 serverHost="..." serverPort=27017 durationMS=42
实现日志记录
要启用日志记录,您还必须在 Cargo.toml
文件中将 跟踪
依赖项中的 log
或 log-always
特性标志添加。您还必须添加对日志记录存储库的依赖,例如 env_logger
[dependencies] tracing = { version = "0.1.37", features = ["log"] } env_logger = "0.10.0" [dependencies.mongodb] version = "3.1.0" features = ["tracing-unstable"]
然后,在您的应用程序中,您必须注册一个全局日志记录器来消费带有日志的事件。以下代码演示了如何注册一个使用 RUST_LOG
环境变量规范的日志记录器
env_logger::init();
如果您在 debug
级别对连接进行日志记录,则驱动程序会在您打开、使用和关闭连接时发出消息。以下代码显示了此日志规范的命令
$ RUST_LOG='mongodb::connection=debug' cargo run
指定了 debug
级别跟踪时,在打开和使用连接时,驱动程序生成日志消息
let my_coll = client.database("db").collection("test_coll"); my_coll.insert_one(doc! { "x" : 1 }).await?;
[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 ...
附加信息
要了解更多关于设置客户端选项的信息,请参阅关于连接选项.