slog-try
Convenience macros for logging with an optional slog Logger.
NOTE - See the bottom of this README for another method of using a Logger that doesn't require Option<Logger>
Current Release
Required dependencies
Add slog-try
as a dependency in your Cargo.toml file.
[]
= "1"
Project setup
Add use statements for the macros you wish to use:
use try_info;
Usage example
Consider HasOptLogger
, a data strcuture with an optionally attachable logger:
The macros contained in slog-try
encapsulate the required boilerplate to use this logger without verifying whether the optional field actually contains a logger or not:
let mut opt_logger = HasOptLogger ;
// Try to log even if no logger exist
try_info!;
try_info!;
// Setup a `Logger`
let plain = new;
let logger = root;
opt_logger.logger = Some;
// Call again with the new attached logger
try_info!;
try_info!;
Using a Discard Logger
You can use slogs Discard
drain in lieu of the Option
wrapped logger. Like the try_*
macros, in case of a none present logger, the discarding logger is going to drop all incoming messages.
How to initialize a discarding logger
use ;