Files
swift-log/Sources/TestApp/LoggerPerSubsystemExample.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

49 lines
1.6 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
import ServerLoggerAPI
private class Client {
private let logger: Logger
init(uuid: UUID, logLevel: LogLevel = .info) {
var logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerPerSubsystemExample.Client")
// NOTE: the log level change below will only affect this `Client` instance which might or might not be what you want.
logger.logLevel = logLevel
logger[diagnosticKey: "UUID"] = uuid.description
self.logger = logger
}
func spawnFreshClient() {
self.logger.info("we're up and running")
let otherSubSystem = OtherSubSystem()
otherSubSystem.randomFunction()
logger.debug("random function returned")
}
}
private class OtherSubSystem {
private let logger: Logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerPerSubsystemExample.OtherSubSystem")
func randomFunction() {
// NOTE: this won't log the UUID as `Client` can't propagate its state to other sub-systems...
self.logger.info("just to say hi")
}
}
enum LoggerPerSubsystemExample {
static func main() {
let logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerPerSubsystemExample.main")
logger.info("main start")
for clientID in 0 ..< 10 {
let client: Client
if clientID % 3 == 0 {
client = Client(uuid: UUID(), logLevel: .debug)
} else {
client = Client(uuid: UUID())
}
client.spawnFreshClient()
}
logger.info("main end")
}
}