#log #logging #macro #macro-derive #function #derive #log-level

macro logcall

An attribute macro that logs the function return value

10 releases

0.1.9 Jun 19, 2024
0.1.8 Jun 19, 2024
0.1.5 Aug 19, 2023
0.1.4 Jul 22, 2023

#570 in Debugging

Download history 1623/week @ 2024-08-15 878/week @ 2024-08-22 1210/week @ 2024-08-29 1178/week @ 2024-09-05 894/week @ 2024-09-12 1256/week @ 2024-09-19 667/week @ 2024-09-26 595/week @ 2024-10-03 1146/week @ 2024-10-10 1018/week @ 2024-10-17 1393/week @ 2024-10-24 1237/week @ 2024-10-31 1197/week @ 2024-11-07 1158/week @ 2024-11-14 1680/week @ 2024-11-21 1012/week @ 2024-11-28

5,236 downloads per month
Used in 5 crates

MIT license

23KB
379 lines

logcall

Crates.io Crates.io Documentation CI Status Crates.io

logcall is a Rust procedural macro crate designed to automatically log function calls, their inputs, and their outputs. This macro facilitates debugging and monitoring by providing detailed logs of function executions with minimal boilerplate code.

This is a re-implementation of the log-derive crate with async-trait compatibility.

Installation

Add logcall to your Cargo.toml:

[dependencies]
logcall = "0.1"

Usage

Import the logcall crate and use the macro to annotate your functions:

use logcall::logcall;

/// Logs the function call at the default `debug` level.
#[logcall]
fn add(a: i32, b: i32) -> i32 {
    a + b
}

/// Logs the function call at the `info` level.
#[logcall("info")]
fn multiply(a: i32, b: i32) -> i32 {
    a * b
}

/// Logs `Ok` results at the `info` level and `Err` results at the `error` level.
#[logcall(ok = "info", err = "error")]
fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err("Division by zero".to_string())
    } else {
        Ok(a / b)
    }
}

/// Logs errors at the `error` level. No log output for `Ok` variant.
#[logcall(err = "error")]
fn divide2(a: usize, b: usize) -> Result<usize, String> {
    if b == 0 {
        Err("Division by zero".to_string())
    } else {
        Ok(a / b)
    }
}

/// Logs the function call with custom input logging format.
#[logcall(input = "a = {a:?}, ..")]
fn subtract(a: i32, b: i32) -> i32 {
    a - b
}

fn main() {
    env_logger::builder()
        .filter_level(log::LevelFilter::Trace)
        .init();

    add(2, 3);
    multiply(2, 3);
    divide(2, 0).ok();
    divide2(2, 0).ok();
    subtract(3, 2);
}

Log Output

When the main function runs, it initializes the logger and logs each function call as specified:

[2024-06-19T15:01:23Z DEBUG main] main::add(a = 2, b = 3) => 5
[2024-06-19T15:01:23Z INFO  main] main::multiply(a = 2, b = 3) => 6
[2024-06-19T15:01:23Z ERROR main] main::divide(a = 2, b = 0) => Err("Division by zero")
[2024-06-19T15:01:23Z ERROR main] main::divide2(a = 2, b = 0) => Err("Division by zero")
[2024-06-19T15:01:23Z DEBUG main] main::subtract(a = 3, ..) => 1

Customization

  • Default Log Level: If no log level is specified, logcall logs at the debug level:
    #[logcall]
    
  • Specify Log Level: Use the macro parameters to specify log level:
    #[logcall("info")]
    
  • Specify Log Levels for Result: Use the ok and err parameters to specify log levels for Ok and Err variants:
    #[logcall(err = "error")]
    #[logcall(ok = "info", err = "error")]
    
  • Customize Input Logging: Use the input parameter to customize the input log format:
    #[logcall(input = "a = {a:?}, ..")]
    #[logcall("info", input = "a = {a:?}, ..")]
    #[logcall(ok = "info", err = "error", input = "a = {a:?}, ..")]
    

Contributing

Contributions are welcome! Please submit pull requests or open issues to improve the crate.

License

This project is licensed under the MIT License.

Dependencies

~1.5MB
~38K SLoC