axum_log_util

Crates.ioaxum_log_util
lib.rsaxum_log_util
version0.1.0
created_at2025-07-03 11:31:39.540336+00
updated_at2025-07-03 11:31:39.540336+00
descriptionA procedural macro crate for automatic logging of Axum HTTP request handlers
homepagehttps://github.com/Matze99/axum-log-util
repositoryhttps://github.com/Matze99/axum-log-util
max_upload_size
id1736156
size8,269
Matthias (Matze99)

documentation

https://docs.rs/axum_log_util

README

Axum Log Util

A Rust procedural macro crate that provides logging utilities for HTTP request handlers.

Overview

axum_log_util is a procedural macro crate designed to automatically add logging capabilities to async HTTP request handler functions. It wraps functions with logging statements that record when endpoints are executed and their response status codes.

Features

  • Automatic Request Logging: Logs when an endpoint function starts executing
  • Response Status Logging: Logs the HTTP status code when the endpoint completes
  • Async Function Support: Designed specifically for async HTTP handlers
  • Zero Configuration: Simply add the attribute macro to your functions

Usage

Add the #[log_request] attribute to your async HTTP handler functions:

use axum_log_util::log_request;

#[log_request]
pub async fn my_endpoint() -> Response {
    // Your endpoint logic here
    Response::new()
}

What it does

The macro transforms your function to include logging statements:

  1. Before execution: Logs "Executing Endpoint: {function_name}"
  2. After execution: Logs "{status_code}: Endpoint {function_name}"

Example Output

INFO: Executing Endpoint: my_endpoint
INFO: 200 OK: Endpoint my_endpoint

Dependencies

  • syn (v2.0): For parsing Rust syntax
  • quote (v1.0): For generating Rust code
  • proc-macro: Standard library for procedural macros

Technical Details

This crate is a procedural macro library (proc-macro = true) that operates at compile time to transform your functions. It:

  1. Parses the input function using syn
  2. Extracts the function name, inputs, and body
  3. Wraps the original function body with logging statements
  4. Returns the modified function using quote!

Requirements

  • Rust 2021 Edition
  • Functions must be async and return a Response type
  • Requires a logging framework (like log, tracing, etc.) to be configured in your application

Development

To test the macro:

cargo test

To see the expanded macro output:

cargo expand
Commit count: 0

cargo fmt