Skip to content

Commit

Permalink
Docs: Info about logging with log ecosystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorak-mmk committed May 14, 2024
1 parent 01afc37 commit 370b00a
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions docs/source/logging/logging.md
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
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=<scylla_ip>: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<dyn Error>> {
// 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=<scylla_ip>:9042 cargo run --example logging_log`.

0 comments on commit 370b00a

Please sign in to comment.