mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
motivation: add tests to confirm desired functionality changes: * add property for logger metadata (aka context) * improve pretty-metadata (aka pretty-context) generation * add unit tests covering local and global use cases * add [test only] implementation of MDC * format with swiftformat open issues: * should we name the new property metadata or context * should we include a default MDC implmentation as part of the logging API. if we dont, we will encourage libraries binding to specific logging implementations that support it (that was bad in java) * tests are a bit brittle * why is ExampleLoggerImplementation a reference type?
35 lines
1.0 KiB
Swift
35 lines
1.0 KiB
Swift
import Foundation
|
|
import ServerLoggerAPI
|
|
|
|
private class Client {
|
|
func spawnFreshClient(uuid: UUID, logger: Logger) {
|
|
var logger = logger
|
|
logger[diagnosticKey: "UUID"] = uuid.description
|
|
logger.info("we're up and running")
|
|
let otherSubSystem = OtherSubSystem()
|
|
otherSubSystem.randomFunction(logger: logger)
|
|
logger.debug("random function returned")
|
|
}
|
|
}
|
|
|
|
private class OtherSubSystem {
|
|
func randomFunction(logger: Logger) {
|
|
logger.debug("this propagates UUID and log level")
|
|
}
|
|
}
|
|
|
|
enum ExplicitContextPassingExample {
|
|
static func main() {
|
|
let logger = LoggerFactory.make(identifier: "com.example.TestApp.ExplicitContextPassingExample.main")
|
|
for clientID in 0 ..< 10 {
|
|
var clientLogger = logger
|
|
if clientID % 3 == 0 {
|
|
clientLogger.logLevel = .debug
|
|
} else {
|
|
clientLogger.logLevel = .info
|
|
}
|
|
Client().spawnFreshClient(uuid: UUID(), logger: clientLogger)
|
|
}
|
|
}
|
|
}
|