Files
swift-log/Sources/ExampleUsage/LocalLoggerBasedSystem.swift
T
tomer doron 9027df5f03 refactor examples and remove dependecy on foundation
motivation:
* due to the low level nature of the library we prefer not to force-load foundation
* help reviewers diffrentiate between examples of usage vs implementation, and add more realistic usage and implementations examples

changes:
* remove foundation from Logging module, replace with c based locks and time functions
* rewrite usage examples to be more realistic
* add example of an implementation that writes to files
* add example of an implementation that is config driven
* rename warn -> warning
2019-01-05 02:03:44 -08:00

91 lines
3.4 KiB
Swift

//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER USAGE LOOKS LIKE
//
import ExampleImplementation
import Foundation
import Logging
// this is a contrived example of a system where each component obtain its own logger
enum LocalLoggerBasedSystem {
static func main() {
// boostrap with our sample implementation
let logging = SimpleLogging()
Logging.bootstrap(logging.make)
// run the example
for i in 1 ... 2 {
print("---------------------------- processing request #\(i) ----------------------------")
Foo().doSomething(requestNumnber: i)
}
}
class Foo {
private var logger = Logging.make("LocalLoggerBasedSystem::Foo")
func doSomething(requestNumnber: Int) {
if 0 == requestNumnber % 2 {
// debug verbosely every 2nd request
self.logger.logLevel = .trace
}
self.logger.info("\(self)::doSomething")
self.doSomethingElse(requestNumnber: requestNumnber)
self.logger.info("\(self)::doSomething::end")
}
private func doSomethingElse(requestNumnber: Int) {
self.logger[metadataKey: "foo"] = "bar" // logger is a value type, changes only effect this instance
self.logger.info("\(self)::doSomethingElse")
self.logger.trace("\(self)::doSomethingElse:someDebugInfo")
Bar().doSomething(requestNumnber: requestNumnber)
self.logger.info("\(self)::doSomethingElse::end")
}
}
class Bar {
private var logger = Logging.make("LocalLoggerBasedSystem::Bar")
func doSomething(requestNumnber: Int) {
if 0 == requestNumnber % 2 {
// debug verbosely every 2nd request
self.logger.logLevel = .trace
}
self.logger[metadataKey: "bar"] = "baz" // logger is a value type, changes only effect this instance
self.logger.info("\(self)::doSomething")
self.logger.trace("\(self)::doSomething:someDebugInfo")
Baz().doSomething(requestNumnber: requestNumnber)
self.logger.info("\(self)::doSomething::end")
}
}
class Baz {
private var logger = Logging.make("LocalLoggerBasedSystem::Baz")
private let queue = DispatchQueue(label: "LocalLoggerBasedSystem::Baz")
func doSomething(requestNumnber: Int) {
if 0 == requestNumnber % 2 {
// debug verbosely every 2nd request
self.logger.logLevel = .trace
}
self.logger.info("\(self)::doSomething")
self.logger.trace("\(self)::doSomething:someDebugInfo")
let group = DispatchGroup()
group.enter()
queue.async {
self.logger.info("\(self)::doSomethingAsync")
// since the library does not know about the context it will loose the metadata
let library = RandomLibrary()
library.doSomething()
library.doSomethingAsync {
group.leave()
}
}
group.wait()
var l = logger // logger is a value type, changes only effect this instance
l[metadataKey: "baz"] = "qux"
l.info("\(self)::doSomething::Local")
logger.info("\(self)::doSomething::end")
}
}
}