2024-06-04 02:38:41 +00:00
|
|
|
use std::future::{ready, Future, Ready};
|
|
|
|
|
|
|
|
use actix_web::{
|
|
|
|
body::MessageBody,
|
|
|
|
dev::{Service, ServiceRequest, ServiceResponse, Transform},
|
|
|
|
http::StatusCode,
|
|
|
|
ResponseError,
|
|
|
|
};
|
|
|
|
|
2024-06-04 03:15:49 +00:00
|
|
|
pub(crate) struct Log {
|
|
|
|
info: bool,
|
|
|
|
}
|
2024-06-04 02:38:41 +00:00
|
|
|
pub(crate) struct LogMiddleware<S> {
|
2024-06-04 03:15:49 +00:00
|
|
|
info: bool,
|
2024-06-04 02:38:41 +00:00
|
|
|
inner: S,
|
|
|
|
}
|
|
|
|
|
2024-06-04 03:15:49 +00:00
|
|
|
impl Log {
|
|
|
|
pub(crate) fn new(info: bool) -> Self {
|
|
|
|
Self { info }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:38:41 +00:00
|
|
|
#[derive(Debug)]
|
2024-06-04 03:15:49 +00:00
|
|
|
pub(crate) struct LogError {
|
|
|
|
info: bool,
|
|
|
|
error: actix_web::Error,
|
|
|
|
}
|
2024-06-04 02:38:41 +00:00
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct LogFuture<F> {
|
2024-06-04 03:15:49 +00:00
|
|
|
info: bool,
|
|
|
|
|
2024-06-04 02:38:41 +00:00
|
|
|
#[pin]
|
|
|
|
inner: F,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct LogBody<B> {
|
2024-06-04 03:15:49 +00:00
|
|
|
info: bool,
|
|
|
|
|
2024-06-04 02:38:41 +00:00
|
|
|
status: Option<StatusCode>,
|
|
|
|
|
|
|
|
#[pin]
|
|
|
|
inner: B,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Transform<S, ServiceRequest> for Log
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>>,
|
|
|
|
S::Future: 'static,
|
|
|
|
S::Error: Into<actix_web::Error>,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<LogBody<B>>;
|
|
|
|
type Error = actix_web::Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = LogMiddleware<S>;
|
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2024-06-04 03:15:49 +00:00
|
|
|
ready(Ok(LogMiddleware {
|
|
|
|
info: self.info,
|
|
|
|
inner: service,
|
|
|
|
}))
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service<ServiceRequest> for LogMiddleware<S>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>>,
|
|
|
|
S::Future: 'static,
|
|
|
|
S::Error: Into<actix_web::Error>,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<LogBody<B>>;
|
|
|
|
type Error = actix_web::Error;
|
|
|
|
type Future = LogFuture<S::Future>;
|
|
|
|
|
|
|
|
fn poll_ready(
|
|
|
|
&self,
|
|
|
|
ctx: &mut core::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Result<(), Self::Error>> {
|
2024-06-04 03:15:49 +00:00
|
|
|
self.inner.poll_ready(ctx).map(|res| {
|
|
|
|
res.map_err(|e| {
|
|
|
|
LogError {
|
|
|
|
info: self.info,
|
|
|
|
error: e.into(),
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
})
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
|
|
|
LogFuture {
|
2024-06-04 03:15:49 +00:00
|
|
|
info: self.info,
|
2024-06-04 02:38:41 +00:00
|
|
|
inner: self.inner.call(req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, B, E> Future for LogFuture<F>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
F: Future<Output = Result<ServiceResponse<B>, E>>,
|
|
|
|
E: Into<actix_web::Error>,
|
|
|
|
{
|
|
|
|
type Output = Result<ServiceResponse<LogBody<B>>, actix_web::Error>;
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Self::Output> {
|
2024-06-04 03:15:49 +00:00
|
|
|
let info = self.info;
|
2024-06-04 02:38:41 +00:00
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
std::task::Poll::Ready(match std::task::ready!(this.inner.poll(cx)) {
|
|
|
|
Ok(response) => {
|
|
|
|
let status = response.status();
|
|
|
|
|
|
|
|
let status = if response.response().body().size().is_eof() {
|
2024-06-04 03:15:49 +00:00
|
|
|
emit(status, info);
|
2024-06-04 02:38:41 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(status)
|
|
|
|
};
|
|
|
|
|
2024-06-04 03:15:49 +00:00
|
|
|
Ok(response.map_body(|_, inner| LogBody {
|
|
|
|
info,
|
|
|
|
status,
|
|
|
|
inner,
|
|
|
|
}))
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
2024-06-04 03:15:49 +00:00
|
|
|
Err(e) => Err(LogError {
|
|
|
|
info,
|
|
|
|
error: e.into(),
|
|
|
|
}
|
|
|
|
.into()),
|
2024-06-04 02:38:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> MessageBody for LogBody<B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
|
|
|
type Error = B::Error;
|
|
|
|
|
|
|
|
fn size(&self) -> actix_web::body::BodySize {
|
|
|
|
self.inner.size()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Option<Result<actix_web::web::Bytes, Self::Error>>> {
|
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
let opt = std::task::ready!(this.inner.poll_next(cx));
|
|
|
|
|
|
|
|
if opt.is_none() {
|
|
|
|
if let Some(status) = this.status.take() {
|
2024-06-04 03:15:49 +00:00
|
|
|
emit(status, *this.info);
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::task::Poll::Ready(opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for LogError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-06-04 03:15:49 +00:00
|
|
|
self.error.fmt(f)
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for LogError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2024-06-04 03:15:49 +00:00
|
|
|
self.error.source()
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for LogError {
|
|
|
|
fn status_code(&self) -> actix_web::http::StatusCode {
|
2024-06-04 03:15:49 +00:00
|
|
|
self.error.as_response_error().status_code()
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
|
2024-06-04 03:15:49 +00:00
|
|
|
let response = self.error.error_response();
|
2024-06-04 02:38:41 +00:00
|
|
|
let status = response.status();
|
|
|
|
|
|
|
|
if response.body().size().is_eof() {
|
2024-06-04 03:15:49 +00:00
|
|
|
emit(status, self.info);
|
2024-06-04 02:38:41 +00:00
|
|
|
response
|
|
|
|
} else {
|
|
|
|
response.map_body(|_, inner| {
|
|
|
|
LogBody {
|
2024-06-04 03:15:49 +00:00
|
|
|
info: self.info,
|
2024-06-04 02:38:41 +00:00
|
|
|
status: Some(status),
|
|
|
|
inner,
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 03:15:49 +00:00
|
|
|
fn emit(status: StatusCode, info: bool) {
|
2024-06-04 02:38:41 +00:00
|
|
|
if status.is_server_error() {
|
|
|
|
tracing::error!("server error");
|
|
|
|
} else if status.is_client_error() {
|
|
|
|
tracing::warn!("client error");
|
|
|
|
} else if status.is_redirection() {
|
|
|
|
tracing::info!("redirected");
|
2024-06-04 03:15:49 +00:00
|
|
|
} else if info {
|
2024-06-04 02:38:41 +00:00
|
|
|
tracing::info!("completed");
|
2024-06-04 03:15:49 +00:00
|
|
|
} else {
|
|
|
|
tracing::debug!("completed");
|
2024-06-04 02:38:41 +00:00
|
|
|
}
|
|
|
|
}
|