1 unstable release
| 0.1.0 | Dec 28, 2025 |
|---|
#465 in Debugging
37KB
272 lines
Loggery
A lightweight, no_std-friendly logging library for Rust.
Usage
Add loggery to your Cargo.toml:
[dependencies]
loggery = "0.1.0"
Then use the logging macros:
use loggery::{trace, debug, info, warn, error};
trace!("This is a TRACE log!");
debug!("This is a DEBUG log!");
info!("This is an INFO log!");
warn!("This is a WARN log!");
error!("This is an ERROR log!");
Output (default logger format):
[TRACE] This is a TRACE log!
[DEBUG] This is a DEBUG log!
[ INFO] This is an INFO log!
[ WARN] This is a WARN log!
[ERROR] This is an ERROR log!
Custom Logger
By default, logs are written in the format: [LEVEL] message,
e.g., [ERROR] Something went wrong!.
But you can implement your own:
use loggery::{Payload, debug};
fn my_logger(payload: Payload) {
// Your custom implementation
// For example, you can change the format of the logger
println!("[APPLICATION]-{}-({})", payload.level.as_str(), payload.args);
}
fn main() {
loggery::set_logger(my_logger);
debug!("A log message using my custom logger!");
}
Output:
[APPLICATION]-DEBUG-(A log message using my custom logger!)
[!NOTE]
set_loggerisn't available if thestaticfeature is enabled! Read Static for more details.
Runtime Level
[!NOTE] Only available when the
runtime_levelfeature is enabled (enabled by default).
You can dynamically change the minimum log level at runtime using set_min_level:
use loggery::{Level, debug, warn};
loggery::set_min_level(Level::Warn);
debug!("This will NOT be logged");
warn!("This will be logged");
This works alongside compile-time filtering using min_level_* features.
Runtime filtering can only be more restrictive, not less restrictive than compile-time feature.
For example if the min_level_info feature is enabled, debug!, trace! calls are removed
at compile-time and cannot be re-enabled at runtime.
Static
[!NOTE] Only available when the
staticfeature is enabled.
For maximum performance or in embedded/performance-critical applications, use the static feature
to remove the runtime indirection. Your logger is linked directly at compile time:
[dependencies]
loggery = { version = "0.1.0", default-features = false, features = ["static"]}
Then define your logger implementation in your binary crate:
use loggery::{Payload, debug};
#[no_mangle]
pub extern "Rust" fn __loggery_log_impl(payload: Payload) {
// Your custom implementation
}
fn main() {
debug!("Direct call from custom static implementation!")
}
[!TIP] The feature combination of
stdandstaticis possible, but you'd still have to define__loggery_log_implfunction. If you want to use the default stdout static definition provided by the crate, usestatic_defaultfeature (enablesstdandstaticfeatures automatically):loggery = { version = "0.1.0", features = ["static_default"] }This gives you direct compile-time linking without needing to define
__loggery_log_impl.
[!NOTE] When using
static_defaultfeature, you cannot define your own__loggery_log_implfunction, as this will cause a duplicate symbol linker error!
[!TIP] Even with
staticfeature, you can still use theruntime_levelfeature and therefore theset_min_levelfunction to do runtime log level filtering.
[!WARNING] When using the
staticfeature, you must define__loggery_log_implfunction in your binary crate, or you'll get a linker error!
Extensions
[!NOTE] Only available when the
extensionfeature is enabled.
Extensions provide a hook for extra processing alongside the actual logger. They're called
before the logger and receive a reference to the Payload, giving you the ability to:
- Save logs to files
- Send logs to external services
- Collect metrics
- etc.
use loggery::{Payload, debug};
fn my_extension(payload: &Payload) {
// Your custom implementation
// For example, you can use the provided extension `save_to_file`
let _ = loggery::extensions::save_to_file(payload, "path/to/app.log");
}
fn main() {
loggery::set_extension(my_extension);
debug!("A log message that will be saved to a file too!");
}
[!NOTE] When the
staticfeature is enabled,set_extensionisn't available. Instead, you must do this:use loggery::Payload; #[no_mangle] pub extern "Rust" fn __loggery_extension_impl(payload: &Payload) { // Your custom implementation }
[!WARNING] When using
staticandextensionfeatures, you must define__loggery_extension_implfunction in your binary crate, or you'll get a linker error!
Features
Default features:
std,metadata,runtime_level
| Feature | Default | Description |
|---|---|---|
std |
✓ | Enables default stdout logger |
static |
✗ | Enables static extern logger definition |
static_default |
✗ | Provides default static logger (enables std + static) |
metadata |
✓ | Enables meta field in the Payload |
extension |
✗ | Enables extension hooks for extra functionality |
runtime_level |
✓ | Allows changing log level filtering at runtime |
min_level_off |
✗ | Disables all logs at compile time |
min_level_trace |
✗ | Only logs trace, debug, info, warn, error |
min_level_debug |
✗ | Only logs debug, info, warn, error |
min_level_info |
✗ | Only logs info, warn, error |
min_level_warn |
✗ | Only logs warn, error |
min_level_error |
✗ | Only logs error |