2 releases
0.1.1 | May 31, 2019 |
---|---|
0.1.0 | May 31, 2019 |
67,944 downloads per month
Used in 2 crates
8KB
Slog Scope for Futures
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
lib.rs
:
Slog Scopes for the Async World
This crate provides a mechanism to use slog scopes with Future
s.
The Problem
With synchronous code, slog-scope works as expected. But what about when
dealing with async
/await
?
This won't compile:
slog_scope::scope(&logger, || {
some_operation().await // Error: can't use await outside of an async fn/block
})
This compiles, but doesn't do what you actually want:
let logger = slog_scope::logger().new(o!("name" => "sub logger"));
let fut = slog_scope::scope(&logger, async || { // <- scope start
some_operation().await
}); // <- scope end
fut.await // Scope not active here while the future is actually running
The Solution
Rather than using a closure to represent a slog scope, the logger must
instead be tied to the future itself, and its poll
method wrapped in
a scope. The SlogScope
type provides a Future
wrapper that does exactly
that.
Usage
Using the wrapper directly:
use slog_scope_futures::SlogScope;
let logger = slog_scope::logger().new(o!("name" => "sub logger"));
SlogScope::new(logger, some_operation()).await
Using the convenience trait:
use slog_scope_futures::FutureExt;
let logger = slog_scope::logger().new(o!("name" => "sub logger"));
some_operation().with_logger(logger).await
Borrowed vs Owned Loggers
Often, you need a Future
to be 'static
so that it can be spawned into
an executor. Other times, though, you can get away with borrowing the
logger. This way, it can be re-used without additional cloning of the
handle.
Because the SlogScope
wrapper takes any L: Borrow<Logger>
, you can
create it with either an owned or a borrowed Logger
.
use slog_scope_futures::FutureExt;
let logger = slog_scope::logger().new(o!("name" => "sub logger"));
some_operation().with_logger(&logger).await; // <- borrowed logger
let fut = some_other_operation().with_logger(logger); // <- owned logger
async fn assert_static<F: Future + 'static>(f: F) -> F::Output { f.await }
assert_static(fut).await
Dependencies
~365KB