|
1 | 1 | # Logging
|
2 | 2 |
|
3 | 3 | The driver uses the [tracing](https://github.com/tokio-rs/tracing) crate for all logs.\
|
4 |
| -To view the logs you have to create a `tracing` subscriber to which all logs will be written. |
| 4 | +There are two ways to view the logs: |
| 5 | +- Create a `tracing` subscriber to which all logs will be written (recommended). |
| 6 | +- Enable `log` feature on `tracing` crate and use some logger from `log` ecosystem. \ |
| 7 | +Only do this if you can't use `tracing` subscriber for some reason. |
| 8 | + |
| 9 | +## Using tracing subscriber |
| 10 | + |
| 11 | +To print the logs you can use the default subscriber: |
5 | 12 |
|
6 |
| -To just print the logs you can use the default subscriber: |
7 | 13 | ```rust
|
8 | 14 | # extern crate scylla;
|
9 | 15 | # extern crate tokio;
|
@@ -45,4 +51,44 @@ To start this example execute:
|
45 | 51 | RUST_LOG=info cargo run
|
46 | 52 | ```
|
47 | 53 |
|
48 |
| -The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging.rs) is available in the `examples` folder |
| 54 | +The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging.rs) is available in the `examples` folder. |
| 55 | +You can run it from main folder of driver repository using `RUST_LOG=trace SCYLLA_URI=<scylla_ip>:9042 cargo run --example logging`. |
| 56 | + |
| 57 | +## Using log |
| 58 | + |
| 59 | +To collect tracing events using log collector you first need to enable `log` feature on `tracing` crate. |
| 60 | +You can use `cargo add tracing -F log` or edit `Cargo.toml`: |
| 61 | +```toml |
| 62 | +tracing = { version = "0.1.40" , features = ["log"] } |
| 63 | +``` |
| 64 | +then you can setup `env_logger` os some other logger and it will output logs from the driver: |
| 65 | + |
| 66 | +```rust |
| 67 | +# extern crate scylla; |
| 68 | +# extern crate tokio; |
| 69 | +# extern crate tracing; |
| 70 | +# extern crate env_logger; |
| 71 | +# use std::error::Error; |
| 72 | +# use scylla::{Session, SessionBuilder}; |
| 73 | +use tracing::info; |
| 74 | + |
| 75 | +#[tokio::main] |
| 76 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 77 | + // Setup `log` collector that uses RUST_LOG env variable to configure |
| 78 | + // verbosity. |
| 79 | + env_logger::init(); |
| 80 | + |
| 81 | + let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string()); |
| 82 | + info!("Connecting to {}", uri); |
| 83 | + |
| 84 | + let session: Session = SessionBuilder::new().known_node(uri).build().await?; |
| 85 | + session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?; |
| 86 | + |
| 87 | + session.query("USE examples_ks", &[]).await?; |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +The full [example](https://github.com/scylladb/scylla-rust-driver/tree/main/examples/logging_log.rs) is available in the `examples` folder. |
| 94 | +You can run it from main folder of driver repository using `RUST_LOG=trace SCYLLA_URI=<scylla_ip>:9042 cargo run --example logging_log`. |
0 commit comments