diff --git a/examples/Cargo.toml b/examples/Cargo.toml index b068ee9e3c..bd56a0798b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -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" } @@ -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" @@ -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" diff --git a/examples/logging_log.rs b/examples/logging_log.rs new file mode 100644 index 0000000000..282d95b8b5 --- /dev/null +++ b/examples/logging_log.rs @@ -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(()) +}