Skip to content

Commit 167229f

Browse files
add logging
1 parent e761235 commit 167229f

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ anyhow = { version = "1.0.95" }
1414
regex = { version = "1.11.1" }
1515
image = { version = "0.25.5"}
1616
dotenvy = { version = "0.15.7"}
17+
18+
[profile.release]
19+
incremental = false
20+
lto = "fat"
21+
opt-level = 3
22+
panic = "abort"
23+
strip = "debuginfo"

src/main.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
use std::time::Duration;
2+
13
use axum::{
2-
http::Method, routing::get, Router
4+
body::Body,
5+
http::{Method, Request, Response},
6+
routing::get,
7+
Router
8+
};
9+
use tower_http::{
10+
cors::{Any, CorsLayer},
11+
trace::TraceLayer
312
};
4-
use tower_http::{cors::{Any, CorsLayer}, trace::TraceLayer};
513
use anyhow::Result;
14+
use tracing::{Level, Span};
15+
use tracing_subscriber::fmt::time;
616
use utils::env::ENV_CONFIG;
717

818

@@ -17,17 +27,39 @@ async fn main() -> Result<()> {
1727
let env_config = ENV_CONFIG.get().expect("Failed to load env config.");
1828

1929
tracing_subscriber::fmt()
20-
.with_max_level(tracing::Level::INFO)
30+
.with_timer(time::time())
31+
.with_target(false)
32+
.with_max_level(Level::INFO)
2133
.init();
2234

35+
let trace = TraceLayer::new_for_http()
36+
.on_request(
37+
|request: &Request<Body>, _span: &Span| {
38+
tracing::info!(
39+
" Incoming [ {} ] {}",
40+
request.method(),
41+
request.uri().path()
42+
);
43+
}
44+
)
45+
.on_response(
46+
|_response: &Response<Body>, _latency: Duration, _span: &Span| {
47+
tracing::info!(
48+
" Outgoing [ {} ] Took {} ms",
49+
_response.status().as_u16(),
50+
_latency.as_millis()
51+
);
52+
}
53+
);
54+
2355
let cors = CorsLayer::new()
2456
.allow_methods([Method::GET])
2557
.allow_origin(Any);
2658

2759
let app = Router::new()
2860
.route("/images/{target}", get(endpoints::legacy_image::handler))
2961
.route("/images/{season}/{episode}/{target}", get(endpoints::image::handler))
30-
.layer(TraceLayer::new_for_http())
62+
.layer(trace)
3163
.layer(cors);
3264

3365
let listener = tokio::net::TcpListener::bind(

0 commit comments

Comments
 (0)