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

49 lines
1.5 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 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")
}
}