tgrapperon / swift-dependencies-additions

More dependencies for `swift-dependencies`
MIT License
298 stars 39 forks source link

Add `OSSignposter` sugar and make `Logger` not fail when testing. #51

Closed tgrapperon closed 1 year ago

tgrapperon commented 1 year ago

The current logger dependency fails when testing. We can't inspect messages that are logged and this dependency is so ubiquitous that it can be cumbersome to have to override it for every tests. It is thus probably preferable to make this dependency work without failing by default when testing. Users who want to assert that no logging occur can override the \.logger dependency with the .unimplemented value.

This commit also exposes some sugar to directly derive an OSSignposter value from a Logger. Its API suffers the same limitations of Logger regarding privacy, so, like Logger, we can't create a configurable/inspectable wrapper of this type. For this reason, the dependency directly produces an OSSignposter value.

You can use the provided subscript to emit signposts on any subsystem/category:

@Dependency(\.logger["Billing"].signpost) var signpost

signpost.emitEvent(
  "Purchase successful",
  "User \(userID, privacy: .private(mask: .hash)) did purchase item \(item, privacy: .auto)"
)