Convenience macros for logging with an optional slog Logger.
Add slog
and slog-try
as a dependency in your Cargo.toml file.
toml
[dependencies]
slog = "^2"
slog-try = "^0.2"
Add the new dependencies as external crates into the main.rs
or lib.rs
file of your project:
```rust
extern crate slog;
extern crate slog_try; ```
Consider HasOptLogger
, a data strcuture with an optionally attachable logger:
```rust
struct HasOptLogger {
logger: Option
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:
```rust let mut opt_logger= HasOptLogger { logger: None };
// Try to log even if no logger exist tryinfo!(optlogger.logger, "You won't see me output. The logger is None."; "opt" => "None"); tryinfo!(optlogger.logger, #"imatag", "You won't see me output. The logger is None."; "opt" => "None");
// Setup a Logger
let plain = slogterm::PlainSyncDecorator::new(::std::io::stdout());
let logger = Logger::root(slogterm::FullFormat::new(plain)
.build()
.fuse(), o!("surname" => "Lava"));
opt_logger.logger = Some(logger);
// Call again with the new attached logger tryinfo!(optlogger.logger, "You will see me output!"; "opt" => "Some"); tryinfo!(optlogger.logger, #"imatag", "You will see me output!"; "opt" => "Some"); ```