diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-10-12 11:00:08 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-10-12 11:00:08 +0100 |
| commit | 8a4544fc183e112564bdc6ac8511152c92ce7c56 (patch) | |
| tree | fe9102440b8c747f2f29902eecf35007b7c93936 /src | |
| parent | bbce03f95d82d5373f2b6a1c28b03eb25d7bb94c (diff) | |
✨feat: add tracingdev
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 4 | ||||
| -rw-r--r-- | src/errors.rs | 10 | ||||
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | src/trace.rs | 7 |
4 files changed, 27 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs index 2f1bbb7..31d8489 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,10 @@ impl Config { let mut config = if config_path.exists() { Config::load_config(config_path)? } else { + tracing::info!( + "Couldn't load configuration from {}, generating defaults", + config_path.display() + ); Config::generate_defaults(config_home)? }; diff --git a/src/errors.rs b/src/errors.rs index 6b53c2d..dc5a034 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -31,6 +31,9 @@ pub enum Error { #[from] ParseIntError(std::num::ParseIntError), + + #[from] + TracingSubEnvError(tracing_subscriber::filter::FromEnvError), } impl core::fmt::Display for Error { @@ -48,6 +51,7 @@ impl core::fmt::Display for Error { Error::TomlDeError(e) => write!(fmt, "TOML deserialization error: {e}"), Error::TomlSerError(e) => write!(fmt, "TOML serialization error: {e}"), Error::ParseIntError(e) => write!(fmt, "Parsing error: {e}"), + Error::TracingSubEnvError(e) => write!(fmt, "Environment variable error: {e}"), } } } @@ -88,9 +92,13 @@ impl IntoResponse for Error { StatusCode::INTERNAL_SERVER_ERROR, format!("Parsing error: {e}"), ), + Error::TracingSubEnvError(ref e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Environment variable error: {e}"), + ), }; - println!("{} {}", &status, &error_message); + tracing::error!("{} {}", &status, &error_message); (status, error_message).into_response() } } @@ -3,6 +3,7 @@ mod config; mod errors; mod handlers; mod models; +mod trace; pub use errors::{Error, Result}; @@ -11,7 +12,7 @@ use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use axum::{Router, routing::get}; use clap::Parser; use tokio::net::TcpListener; -use tower_http::services::ServeFile; +use tower_http::{services::ServeFile, trace::TraceLayer}; use args::{Cli, Command}; use config::Config; @@ -21,6 +22,7 @@ pub fn app(config: Config) -> Router { .nest_service("/static/style.css", ServeFile::new(&config.style_file)) .nest_service("/static/favicon.svg", ServeFile::new(&config.favicon_file)) .route("/", get(handlers::handler)) + .layer(TraceLayer::new_for_http()) .with_state(config) } @@ -31,8 +33,12 @@ pub async fn run() -> Result<()> { Command::Serv(args) => { let config = config::Config::new(args)?; + trace::tracing_registry(); + let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), config.port); + let listener = TcpListener::bind(socket).await?; + tracing::info!("listening on {}", listener.local_addr()?); axum::serve(listener, app(config.clone())).await?; } diff --git a/src/trace.rs b/src/trace.rs new file mode 100644 index 0000000..0b0eb23 --- /dev/null +++ b/src/trace.rs @@ -0,0 +1,7 @@ +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; + +pub fn tracing_registry() { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().without_time()) + .init(); +} |
