1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! This examples shows how to create log files.
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --example client_logger
//! ```
use iota_sdk::client::{Client, Result};
#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();
for var in ["NODE_URL"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
// Generates a client.log file with logs for debugging.
// We exclude logs from h2, hyper and rustls to reduce the noise.
let logger_output_config = fern_logger::LoggerOutputConfigBuilder::new()
.name("client.log")
.target_exclusions(&["h2", "hyper", "rustls"])
.level_filter(log::LevelFilter::Debug);
let config = fern_logger::LoggerConfig::build()
.with_output(logger_output_config)
.finish();
fern_logger::logger_init(config).unwrap();
// Create a node client.
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
.finish()
.await?;
// Get node info.
let _ = client.get_info().await?;
println!("Example completed successfully. `client.log` file has been updated.");
Ok(())
}