swift-log icon indicating copy to clipboard operation
swift-log copied to clipboard

Metadata providers

Open slashmo opened this issue 4 years ago • 1 comments

While it's currently possible to have "semi-global" metadata by attaching metadata values to a Logger itself there's no notion of globally stating that the current value of something should always be included in log statements.

This would be great to have for example for tracing where the user may want to include the current trace ID in each log statement. Instead of manually pulling out the trace ID (from task locals) before each log statement we could make it a bit easier by having a closure to be invoked for each statement automatically. In that closure the user could look up and return the current trace ID. Also, multiple of such closures could be provided so that e.g. a tracer implementation could take care of providing the trace ID, and the user could provide something application-specific which is also stored in a task local.

// example of a tracer invoking `provideMetadata`
// (`Baggage.current` is a task-local)

LoggingSystem.provideMetadata {
    return Baggage.current.traceID.map { ["trace-id": "\($0)"] }
}

slashmo avatar Aug 13 '21 10:08 slashmo

Right, this design will allow us to pick and choose any metadata from task context that someone may want to log. Even values provided by the system or anything else really. 👍

ktoso avatar Aug 13 '21 10:08 ktoso

resolved by https://github.com/apple/swift-log/pull/238

ktoso avatar Jan 18 '23 20:01 ktoso

@ktoso Funny how we ended up with almost this design, not passing Baggage to the MetadataProvider directly 😊

slashmo avatar Jan 19 '23 09:01 slashmo

Yeah pretty much hah :) it turned out quite well!

ktoso avatar Jan 19 '23 09:01 ktoso