From 192f0e757aae18f20c9b5bde10e5a92982e6a42d Mon Sep 17 00:00:00 2001 From: Lyle Mantooth Date: Sun, 14 May 2023 11:07:30 -0400 Subject: [PATCH] Add tracing. --- Cargo.lock | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 17 +++++++++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index ee07cac..df9494b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,8 @@ dependencies = [ "reqwest", "serde", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] @@ -936,6 +938,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -1041,6 +1053,12 @@ version = "6.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "owo-colors" version = "3.5.0" @@ -1628,9 +1646,21 @@ dependencies = [ "cfg-if", "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.31" @@ -1651,15 +1681,42 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ + "nu-ansi-term", + "serde", + "serde_json", "sharded-slab", + "smallvec", "thread_local", "tracing-core", + "tracing-log", + "tracing-serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d1d07c4..0158c5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,7 @@ pretty-hex = "0.3" reqwest = { version = "0.11", features = ["json"] } serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["json"] } [dev-dependencies] diff --git a/src/main.rs b/src/main.rs index 6627ba1..bdb6840 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use axum::{ body::BoxBody, http::header, @@ -7,12 +9,25 @@ use axum::{ }; use reqwest::StatusCode; use serde::Deserialize; +use tracing::{info, Level}; +use tracing_subscriber::{filter::Targets, layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { + let filter = Targets::from_str(std::env::var("RUST_LOG").as_deref().unwrap_or("info")) + .expect("RUST_LOG should be a valid tracing filter"); + tracing_subscriber::fmt() + .with_max_level(Level::TRACE) + .json() + .finish() + .with(filter) + .init(); + let app = Router::new().route("/", get(root_get)); - axum::Server::bind(&"0.0.0.0:8080".parse().unwrap()) + let addr = "0.0.0.0:8080".parse().unwrap(); + info!("Listening on {addr}"); + axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap();