From 370b00a9fda4f76149be02ffc006961928752960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Bary=C5=82a?= Date: Thu, 2 May 2024 17:21:39 +0200 Subject: [PATCH] Docs: Info about logging with `log` ecosystem. --- docs/source/logging/logging.md | 52 ++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/source/logging/logging.md b/docs/source/logging/logging.md index 198b3291f4..c99d1b1e0f 100644 --- a/docs/source/logging/logging.md +++ b/docs/source/logging/logging.md @@ -1,9 +1,15 @@ # Logging The driver uses the [tracing](https://github.com/tokio-rs/tracing) crate for all logs.\ -To view the logs you have to create a `tracing` subscriber to which all logs will be written. +There are two ways to view the logs: +- Create a `tracing` subscriber to which all logs will be written (recommended). +- Enable `log` feature on `tracing` crate and use some logger from `log` ecosystem. \ +Only do this if you can't use `tracing` subscriber for some reason. + +## Using tracing subscriber + +To print the logs you can use the default subscriber: -To just print the logs you can use the default subscriber: ```rust # extern crate scylla; # extern crate tokio; @@ -45,4 +51,44 @@ To start this example execute: RUST_LOG=info cargo run ``` -The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging.rs) is available in the `examples` folder \ No newline at end of file +The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging.rs) is available in the `examples` folder. +You can run it from main folder of driver repository using `RUST_LOG=trace SCYLLA_URI=:9042 cargo run --example logging`. + +## Using log + +To collect tracing events using log collector you first need to enable `log` feature on `tracing` crate. +You can use `cargo add tracing -F log` or edit `Cargo.toml`: +```toml +tracing = { version = "0.1.40" , features = ["log"] } +``` +then you can setup `env_logger` os some other logger and it will output logs from the driver: + +```rust +# extern crate scylla; +# extern crate tokio; +# extern crate tracing; +# extern crate env_logger; +# use std::error::Error; +# use scylla::{Session, SessionBuilder}; +use tracing::info; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Setup `log` collector that uses RUST_LOG env variable to configure + // verbosity. + env_logger::init(); + + let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string()); + info!("Connecting to {}", uri); + + let session: Session = SessionBuilder::new().known_node(uri).build().await?; + session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?; + + session.query("USE examples_ks", &[]).await?; + + Ok(()) +} +``` + +The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging_log.rs) is available in the `examples` folder. +You can run it from main folder of driver repository using `RUST_LOG=trace SCYLLA_URI=:9042 cargo run --example logging_log`. \ No newline at end of file