Skip to content

Commit

Permalink
feat: [#468] deafult logging for API requests
Browse files Browse the repository at this point in the history
This change enables the default logging for HTTP requests to the API:

```
2024-02-12T14:15:15.546548923+00:00 [tower_http::trace::make_span][INFO] request; method=GET uri=/v1/about version=HTTP/1.1
2024-02-12T14:15:15.546840151+00:00 [tower_http::trace::on_response][INFO] finished processing request latency=0 ms status=200
2024-02-12T14:15:34.754336133+00:00 [tower_http::trace::make_span][INFO] request; method=GET uri=/v1/about/license version=HTTP/1.1
2024-02-12T14:15:34.754544571+00:00 [tower_http::trace::on_response][INFO] finished processing request latency=0 ms status=200
```
  • Loading branch information
josecelano committed Feb 12, 2024
1 parent c1e01f8 commit e5ebcc0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/web/api/server/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use axum::{Json, Router};
use serde_json::{json, Value};
use tower_http::compression::CompressionLayer;
use tower_http::cors::CorsLayer;
use tower_http::trace::{DefaultMakeSpan, DefaultOnRequest, DefaultOnResponse, TraceLayer};
use tracing::Level;

use super::contexts::{about, category, proxy, settings, tag, torrent, user};
use crate::bootstrap::config::ENV_VAR_CORS_PERMISSIVE;
Expand Down Expand Up @@ -37,7 +39,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {

let router = Router::new()
.route("/", get(redirect_to_about))
.route("/health_check", get(health_check_handler).with_state(app_data))
.route("/health_check", get(health_check_handler).with_state(app_data.clone()))
.nest(&format!("/{API_VERSION_URL_PREFIX}"), v1_api_routes);

let router = if env::var(ENV_VAR_CORS_PERMISSIVE).is_ok() {
Expand All @@ -46,7 +48,15 @@ pub fn router(app_data: Arc<AppData>) -> Router {
router
};

router.layer(DefaultBodyLimit::max(10_485_760)).layer(CompressionLayer::new())
router
.layer(DefaultBodyLimit::max(10_485_760))
.layer(CompressionLayer::new())
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(DefaultOnResponse::new().level(Level::INFO)),
)
}

/// Endpoint for container health check.
Expand Down

0 comments on commit e5ebcc0

Please sign in to comment.