Loggery
A lightweight, no_std-friendly logging library for Rust.
Usage
Add loggery to your Cargo.toml:
[]
= "0.1.0"
Then use the logging macros:
use ;
trace!;
debug!;
info!;
warn!;
error!;
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 ;
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 ;
set_min_level;
debug!;
warn!;
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:
[]
= { = "0.1.0", = false, = ["static"]}
Then define your logger implementation in your binary crate:
use ;
pub extern "Rust"
[!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):= { = "0.1.0", = ["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 ;
[!NOTE] When the
staticfeature is enabled,set_extensionisn't available. Instead, you must do this:use Payload; pub extern "Rust"
[!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 |