effectful_logger 0.2.1

Effect-logging service (forwards to tracing) for effect! / AsyncEffect programs
Documentation
//! Provide [`EffectLogger`] via a concrete [`Context`], then extract it inside
//! `effect!` with `bind* EffectLogger` and use its methods with bind*.
//!
//! Run: `devenv shell -- cargo run -p logger --example context_service`

use ::effectful::{Cons, Context, Effect, Nil, Service, effect, run_blocking};
use effectful_logger::{EffectLogKey, EffectLogger, EffectLoggerError};

type LogR = Context<Cons<Service<EffectLogKey, EffectLogger>, Nil>>;

fn build_ctx() -> LogR {
  Context::new(Cons(Service::<EffectLogKey, _>::new(EffectLogger), Nil))
}

fn main() {
  tracing_subscriber::fmt()
    .with_env_filter(tracing_subscriber::EnvFilter::new("info"))
    .init();

  let prog: Effect<(), EffectLoggerError, LogR> = effect!(|_r: &mut LogR| {
    let logger = bind * EffectLogger;
    bind * logger.info("resolved EffectLogger via Context + provide_service");
    bind * logger.warn("warn from the same extracted logger");
  });

  run_blocking(prog, build_ctx()).expect("EffectLoggerError is never produced by tracing");
}