Files
swift-log/Sources/TestApp/ExplicitLoggerPassingExample.swift
T
tomer doron bf76e03ea3 logger metadata support and unit tests
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?
2018-12-03 18:32:51 -08:00

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)
}
}
}