[][src]Crate slog_scope_futures

Slog Scopes for the Async World

This crate provides a mechanism to use slog scopes with Futures.

The Problem

With synchronous code, slog-scope works as expected. But what about when dealing with async/await?

This won't compile:

This example deliberately fails to 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

Structs

SlogScope

A Future wrapped in a slog scope.

Traits

FutureExt

Convenience trait for wrapping a Future in a slog scope via method chaining.