Effect CommunityEC
Effect Community2mo ago
8 replies
omerT

Providing Log Spans for Different Parts of a Program

How can we provide log spans for different parts of the program?

https://effect.website/docs/observability/logging/#log-spans

This is the example:
import { Effect } from "effect"

const program = Effect.gen(function* () {
  // Simulate a delay to represent a task taking time
  yield* Effect.sleep("1 second")
  // Log a message indicating the job is done
  yield* Effect.log("The job is finished!")
}).pipe(
  // Apply a log span labeled "myspan" to measure
  // the duration of this operation
  Effect.withLogSpan("myspan")
)

Effect.runFork(program)
/*
Output:
timestamp=... level=INFO fiber=#0 message="The job is finished!" myspan=1011ms
*/


How can I do this:
const program2 = Effect.gen(function* () {
    // Simulate a delay to represent a task taking time
    yield* Effect.sleep('1 second')
    // Log a message indicating the job is done
    yield* Effect.log('The job is finished!')

    // Another Task
    yield* Effect.sleep('2 seconds')
    // Log a message indicating the job is done
    yield* Effect.log('The second job is finished!')
}).pipe(
    // Apply a log span labeled "myspan" to measure
    // the duration of this operation
    Effect.withLogSpan('myspan')
)
Effect Documentation
Discover Effect's logging utilities for dynamic log levels, custom outputs, and fine-grained control over logs.
Logging
Was this page helpful?