Defining a logger
Before we write any code, we must have the following dependencies in our | workspace:
# glue/Cargo.toml
. . .
[dependencies]
. . .
tracing = "0.1.4"
tracing-subscriber = "0.3.18"
futures-util = "0.3.30"
We will be using the tracing
crate to define some logging functions, and the tracing-subscriber
to define a logger that will subscribe to the log events being produced. Our basic logger is just printing to the terminal. Why are we not just using println!
? If we use println!(“hello world”)
, we are essentially running the following code:
use std::io::{stdout, Write};
let mut lock = stdout().lock();
write!(lock, "hello world").unwrap();
Here we can see that we are locking the standard output and writing to it. This makes sense as printing to the terminal would not be very useful if half of one message got printed alongside half of another message. This lock mechanism results in a reduction in performance. For instance...