Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the Tracing Crate in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [4]:
:dep thiserror = "1.0.25"
In [6]:
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ParseRankError {
    #[error("{0} is not a valid symbol for card rank!")]
    InvalidSymbol(char),
    #[error("{0} is not a valid integer for card rank!")]
    InvalidInteger(u8),
}
In [2]:
:dep tracing = "0.1.26"
In [3]:
use tracing::{debug, error, info, span, warn, Level};
In [7]:
let e = ParseRankError::InvalidSymbol('m');
e
Out[7]:
InvalidSymbol('m')
In [8]:
e.to_string()
Out[8]:
"m is not a valid symbol for card rank!"
In [17]:
#[tracing::instrument]
fn f(){
    let e = ParseRankError::InvalidSymbol('m');
    warn!("{}", e);
}
f()
Out[17]:
()
In [12]:
error!("{}", e);
In [14]:
info!("{}", e);
In [18]:
use tracing::error;

let (err_info, port) = ("No connection", 22);

error!(port, error = %err_info);
error!(target: "app_events", "App Error: {}", err_info);
error!({ info = err_info }, "error on port: {}", port);
In [ ]:

Comments