axum-log
v0.0.1Basic log middleware for axum api application.
xnex install axum-log GitHub repo ๐ฆ Axum Log Recipe
A zero-dependency, pluggable logging middleware recipe for your modular Axum backend. This recipe sets up a dedicated common/middlewares directory layer, allowing developers to inspect incoming HTTP requests, status codes, and execution latencies instantly without adding heavy third-party tracing binaries.
๐ Architecture & File Tree
This recipe builds smoothly on top of your core workspace, introducing a nested common layout designed for system-wide, shared utility behaviors.
src/
โโโ main.rs
โโโ lib.rs
โโโ routes.rs # Central gateway where the logging layer is mounted
โโโ common/ # Shared cross-cutting concerns layer
โ โโโ mod.rs # Exports internal subfolders (e.g., middlewares)
โ โโโ middlewares/
โ โโโ mod.rs # Registration hub for all custom middlewares
โ โโโ log_middleware.rs # Core logging function implementation
โโโ modules/
โโโ health/ # Standalone feature modules
๐ ๏ธ Code Implementation
1. The Core Middleware (src/common/middlewares/log_middleware.rs)
Intercepts the request, benchmarks lifecycle metrics, logs details to the stdout terminal, and passes the context to the next internal node.
use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;
use std::time::Instant;
pub async fn middleware(request: Request, next: Next) -> Response {
let start = Instant::now();
let method = request.method().clone();
let uri = request.uri().clone();
let response = next.run(request).await;
let duration = start.elapsed();
let status = response.status();
println!("๐ฏ [{}] {} -> {} ({:?})", method, uri, status, duration);
response
}
2. Global Integration (src/routes.rs)
Inject the logger globally across all structural endpoints via axum::middleware::from_fn. Because Axum execution flows from bottom to top, registering your layer at the end ensures all traffic hits the logging pipe first.
#[path = "common/mod.rs"]
mod common;
#[path = "modules/mod.rs"]
mod modules;
use axum::Router;
pub fn create_router() -> Router {
Router::new()
.nest("/api/health", modules::health::router())
.layer(axum::middleware::from_fn(
common::middlewares::log_middleware::middleware,
))
}
๐ Execution & Sample Terminal Logs
Boot the engine without needing to pass external RUST_LOG parameters:
xnex install axum-base axum-log -y -s
cargo run
Output Trace
Once your requests navigate through the middleware stack, the console prints isolated metrics tracking exact execution milestones:
๐ Server started: http://127.0.0.1:3001
๐ฏ [GET] / -> 404 Not Found (34.3ยตs)
๐ฏ [GET] /favicon.ico -> 404 Not Found (154ยตs)
๐ฏ [GET] /api/health -> 200 OK (130.3ยตs)
Versions
Release links are tracked by Nex; source code lives in the developer's GitHub repo.
setup.files
Files that will be integrated into your project
- recipes
- axum-log
- README.md โ recipes/axum-log
-
-
- src
- common
- middlewares
- log_middleware.rs โ src/common/middlewares
- mod.rs โ src/common/middlewares
-
- mod.rs โ src/common
-
-
nexconfig
Full recipe configuration
{
"name": "axum-log",
"version": "0.0.1",
"description": "Basic log middleware for axum api application.",
"keywords": [
"axum",
"nex",
"backend",
"log",
"api",
"recipe"
],
"files": [
"src/common",
"recipes/axum-log/README.md"
],
"setup": {
"files": {
"src/common": {
"middlewares": {
"log_middleware.rs": "src/common/middlewares",
"mod.rs": "src/common/middlewares"
},
"mod.rs": "src/common"
},
"recipes/axum-log/README.md": "recipes/axum-log"
}
}
}
You might also be interested in these
axum-base
Basic Axum starter file archiecture recipe
nexcord-log
Discord bot Message & Voice log bot recipe
nestjs
NESTJS EMPTY TEMPLATE
spring-logger
Basic logger middleware recipe
nestjs-basic-auth
NESTJS BASIC AUTH RECIPE