Metadata providers
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)"] }
}
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. 👍
resolved by https://github.com/apple/swift-log/pull/238
@ktoso Funny how we ended up with almost this design, not passing Baggage to the MetadataProvider directly 😊
Yeah pretty much hah :) it turned out quite well!