github apple/swift-log 1.5.0

latest releases: 1.6.1, 1.6.0, 1.5.4...
22 months ago

Changes

Swift version support

This release drops support for Swift 5.0.

Swift 5.1+ remain supported for the time being.

Logger.MetadataProvider

This release introduces metadata providers!

They are an additional way to add metadata to your log statements automatically whenever a log statement is about to be made. This works extremely well with systems like distributed tracing, that may pick up trace identifiers and other information from the task-local context from where the log statement is being made.

The feature came with a swift evolution style proposal introduction to the "why?" and "how?" of this feature you may find interesting.

Metadata providers are used like this:

import Logging

enum Namespace { 
  @TaskLocal static var simpleTraceID: String?
}

let simpleTraceIDMetadataProvider = Logger.MetadataProvider { 
    guard let traceID = Namespace.simpleTraceID else {
        return [:]
    }
    return ["simple-trace-id": .string(traceID)]
 }

LoggingSystem.bootstrap({ label, metadataProvider in
    myCoolLogHandler(label: label, metadataProvider: metadataProvider)
}, metadataProvider: simpleTraceIDMetadataProvider)

which in turn makes every Logger on this LoggingSystem add this contextual metadata to log statements automatically:

let log = Logger(label: "hello")

Namespace.$simpleTraceID.withValue("1234-5678") {
  test()
}

func test() {
  log.info("test log statement")
}

// [info] [simple-trace-id: 1234-5678] test log statement

Adoption in LogHandlers

In order to support this new feature in your log handlers, please make it accept a MetadataProvider? at creation, and store it as:

struct MyHandler: LogHandler {
    // ... 
    public var metadataProvider: Logger.MetadataProvider?
    // ...
}

What's Changed

Highlight

  • Metadata Providers (e.g. for Distributed Tracing) in LogHandlers by @ktoso in #238

Other changes

  • [docs] Minimal docc setup and landing page by @ktoso in #226
  • =docc Make docs use symbol references by @ktoso in #230
  • =docc Move to multiple Package.swift files by @ktoso in #231
  • Undo 5.7 package files, not needed yet by @ktoso in #232
  • Update README: Add missing Source param by @rusik in #233
  • Fix build for wasm by @ahti in #236
  • Add .spi.yml for Swift Package Index DocC support by @yim-lee in #240
  • Fixes link to Supabase repository in README.md by @timobollwerk in #245

New Contributors

Full Changelog: 1.4.4...1.5.0

Don't miss a new swift-log release

NewReleases is sending notifications on new releases.