funlog
A Rust attribute macro for function logging with configurable options.
Features
- Log function entry and exit with file and line information
- Parameter value logging with type information
- Return value logging with type information
- Support for Result and Option types
- Generic type support with Debug trait
- Configurable log levels (debug, info, warn, error, trace)
- Output length limits for parameters and return values
- Support for nested function calls
- Module path information in logs
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= "0.4"
And initialize a logger in your main function:
Usage Examples
Basic Function Logging
use funlog;
Output:
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:4 my_app::add(a: 5, b: 3) begin
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:4 my_app::add(a: 5, b: 3)->8 end
Result Type Handling
Output:
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:16 my_app::new(name: "Alice", age: 25) begin
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:16 my_app::new(name: "Alice", age: 25)->Ok(Person { name: "Alice", age: 25 }) end
Option Type Handling
Output:
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:32 my_app::new(title: "The Great Gatsby") begin
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:32 my_app::new(title: "The Great Gatsby")->Some(Book { title: "The Great Gatsby" }) end
Generic Functions
Output:
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:42 my_app::process_data(data: [1, 2, 3, 4, 5]) begin
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:42 my_app::process_data(data: [1, 2, 3, 4, 5])->Ok(Some([1, 2, 3, 4, 5])) end
Complex Type Handling
Output:
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:51 my_app::handle_result(result: Ok(Some("Hello"))) begin
[2024-11-29T13:31:35Z INFO my_app] src/main.rs:51 my_app::handle_result(result: Ok(Some("Hello")))->Some(Ok("Hello")) end
Log Level Configuration
Output:
[2024-11-29T13:31:35Z DEBUG my_app] src/main.rs:60 my_app::debug_function(x: 10) begin
[2024-11-29T13:31:35Z DEBUG my_app] src/main.rs:60 my_app::debug_function(x: 10)->20 end
[2024-11-29T13:31:35Z WARN my_app] src/main.rs:65 my_app::warn_function(x: 10) begin
[2024-11-29T13:31:35Z WARN my_app] src/main.rs:65 my_app::warn_function(x: 10)->30 end
Notes
- For generic types and custom structs, the
Debugtrait must be implemented:
-
The macro automatically handles:
- Result types (Ok/Err)
- Option types (Some/None)
- Generic types (with Debug trait)
- Custom structs (with Debug trait)
-
Log format includes:
- Timestamp
- Log level
- Module path
- File name and line number
- Function name
- Parameter names and values
- Return value
-
Available log levels:
- trace
- debug
- info (default)
- warn
- error
License
This project is licensed under the MIT License - see the LICENSE file for details.