MyGit

1.5.0

apple/swift-log

版本发布时间: 2023-01-18 15:42:15

apple/swift-log最新发布版本:1.6.1(2024-06-25 01:37:08)

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

Other changes

New Contributors

Full Changelog: https://github.com/apple/swift-log/compare/1.4.4...1.5.0

相关地址:原始地址 下载(tar) 下载(zip)

查看:2023-01-18发行的版本