Files
swift-log/Sources/Examples/ExplicitLoggerPassingExample.swift
T
tomer doron b5dcb3140e naming proposal (#2)
* naming proposal

motivation: intuitive APIs

changes:
* Logger is the main API users interact with
* Logging is the factory from which users get instances of Logger, via Logging.for(identifer)
* LogHandler is what logging libraries implement, this is not exposed to users as its boxed in Logger
* replace Logging (aka LoggerFactory) factory property with bootstrap(factory) function
* rename main module to Logging since its not server only
* converge examples into an Examples module

open questions:
* Logging.make or Logging.for
* is Logging.for(object: Any.Type) a good idea? is there a better way to get the FQN for an object?
2018-12-10 15:11:03 -08:00

35 lines
1023 B
Swift

import Foundation
import Logging
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 = Logging.make("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)
}
}
}