mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
* 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?
49 lines
1.5 KiB
Swift
49 lines
1.5 KiB
Swift
import Foundation
|
||
import Logging
|
||
|
||
private class Client {
|
||
private let logger: Logger
|
||
|
||
init(uuid: UUID, logLevel: LogLevel = .info) {
|
||
var logger = Logging.make("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 = Logging.make("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 = Logging.make("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")
|
||
}
|
||
}
|