Skip to content

Commit

Permalink
Examples: add logging_log example
Browse files Browse the repository at this point in the history
This example demonstrates usage of `env_logger` to listen to driver logs.
  • Loading branch information
Lorak-mmk committed May 2, 2024
1 parent 1e9cb57 commit e533799
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rustyline = "9"
rustyline-derive = "0.6"
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03", "num-bigint-04", "bigdecimal-04"]}
tokio = {version = "1.1.0", features = ["full"]}
tracing = "0.1.25"
tracing = { version = "0.1.25" , features = ["log"] }
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
chrono = { version = "0.4", default-features = false }
time = { version = "0.3.22" }
Expand All @@ -21,6 +21,7 @@ tower = "0.4"
stats_alloc = "0.1"
clap = { version = "3.2.4", features = ["derive"] }
rand = "0.8.5"
env_logger = "0.10"

[[example]]
name = "auth"
Expand All @@ -34,6 +35,10 @@ path = "basic.rs"
name = "logging"
path = "logging.rs"

[[example]]
name = "logging_log"
path = "logging_log.rs"

[[example]]
name = "tls"
path = "tls.rs"
Expand Down
27 changes: 27 additions & 0 deletions examples/logging_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Result;
use scylla::transport::session::Session;
use scylla::SessionBuilder;
use std::env;
use tracing::info;

// To run this example, and view logged messages, RUST_LOG env var needs to be set
// This can be done using shell command presented below
// RUST_LOG=info cargo run --example logging_log
#[tokio::main]
async fn main() -> Result<()> {
// Driver uses tracing for logging purposes, but it's possible to use `log`
// ecosystem to view the messages. This requires adding `tracing` crate to
// dependencies and enabling it's "log" feature. Then you will be able to use
// loggers like `env_logger` to see driver's messages.
env_logger::init();

let uri = 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(())
}

0 comments on commit e533799

Please sign in to comment.