1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use axum::{body::Body, extract::Request, response::Response};
use futures::future::BoxFuture;
use http::StatusCode;
use std::task::{Context, Poll};
use tower::{Layer, Service};
use tracing::{error, info};
pub enum ServerFnLoggingStatus {
Informational,
Success,
Redirection,
ClientError,
ServerError,
}
impl From<StatusCode> for ServerFnLoggingStatus {
fn from(status: StatusCode) -> Self {
if status.is_informational() {
ServerFnLoggingStatus::Informational
} else if status.is_success() {
ServerFnLoggingStatus::Success
} else if status.is_redirection() {
ServerFnLoggingStatus::Redirection
} else if status.is_client_error() {
ServerFnLoggingStatus::ClientError
} else {
ServerFnLoggingStatus::ServerError
}
}
}
/// Add to your app:
/// ```
/// let app = Router::new()
/// .layer(ServerFnLoggingLayer)
/// // ... rest of your routes
/// ```
#[derive(Clone)]
pub struct ServerFnLoggingLayer;
impl<S> Layer<S> for ServerFnLoggingLayer {
type Service = ServerFnLoggingService<S>;
fn layer(&self, inner: S) -> Self::Service {
ServerFnLoggingService { inner }
}
}
#[derive(Clone)]
pub struct ServerFnLoggingService<S> {
inner: S,
}
impl<S> Service<Request<Body>> for ServerFnLoggingService<S>
where
S: Service<Request<Body>, Response = Response> + Send + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
let path = req.uri().path().to_string();
let method = req.method().clone();
let future = self.inner.call(req);
Box::pin(async move {
let start = std::time::Instant::now();
let result = future.await;
let duration = start.elapsed();
match &result {
Ok(response) => {
let status: StatusCode = response.status();
let statustype: ServerFnLoggingStatus = status.into();
// Note: Axum's Body type doesn't allow easy access to content without consuming it
// To print response bodies, consider using a different middleware approach or
// implement body printing at the handler level
tracing::debug!("Response status: {} for path: {}", status, path);
match statustype {
ServerFnLoggingStatus::Informational => {
info!(
path = %path,
method = %method,
status = %status,
duration = ?duration,
""
);
}
ServerFnLoggingStatus::Success => {
info!(
path = %path,
method = %method,
status = %status,
duration = ?duration,
""
);
}
ServerFnLoggingStatus::Redirection => {
info!(
path = %path,
method = %method,
status = %status,
duration = ?duration,
""
);
}
ServerFnLoggingStatus::ClientError => {
error!(
path = %path,
method = %method,
status = %status,
duration = ?duration,
""
);
}
ServerFnLoggingStatus::ServerError => {
error!(
path = %path,
method = %method,
status = %status,
duration = ?duration,
""
);
}
}
}
Err(_) => {
error!(
path = %path,
method = %method,
duration = ?duration,
"Server function failed"
);
}
}
result
})
}
}