mirror of
https://github.com/apple/swift-log
synced 2026-11-23 03:44:22 +00:00
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
This commit is contained in:
1 parent
0f23cda751
commit
9027df5f03
25 files changed
+858
-381
No files matched your search
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// 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 context based system, ie a system that explictly passes a context object around
|
||||
// in this example we include a logger in the context object
|
||||
// we also allow users to set metadata on the context object and reuse that as the metadata for logging
|
||||
enum ContextBasedSystem {
|
||||
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) ----------------------------")
|
||||
let context = Context(metadata: ["requestId": UUID().uuidString]) // contrived context
|
||||
Foo().doSomething(context: context, requestNumnber: i)
|
||||
}
|
||||
}
|
||||
|
||||
struct Context {
|
||||
var logger = Logging.make("ContextLogger")
|
||||
|
||||
init(metadata: [String: String] = [:]) {
|
||||
metadata.forEach { self[$0.0] = $0.1 }
|
||||
}
|
||||
|
||||
// since logger is a value type, we can reuse our copy to manage the metadata
|
||||
subscript(metadataKey: String) -> String? {
|
||||
get { return self.logger[metadataKey: metadataKey] }
|
||||
set { self.logger[metadataKey: metadataKey] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
class Foo {
|
||||
func doSomething(context: Context, requestNumnber: Int) {
|
||||
var c = context // context is a value type, changes only effect this instance
|
||||
if 0 == requestNumnber % 2 {
|
||||
// debug verbosely every 2nd request
|
||||
c.logger.logLevel = .trace
|
||||
}
|
||||
c.logger.info("\(self)::doSomething")
|
||||
self.doSomethingElse(context: c)
|
||||
c.logger.info("\(self)::doSomething::end")
|
||||
}
|
||||
|
||||
private func doSomethingElse(context: Context) {
|
||||
var c = context // context is a value type, changes only effect this instance
|
||||
c["foo"] = "bar"
|
||||
c.logger.info("\(self)::doSomethingElse")
|
||||
c.logger.trace("\(self)::doSomethingElse:someDebugInfo")
|
||||
Bar().doSomething(context: c)
|
||||
c.logger.info("\(self)::doSomethingElse::end")
|
||||
}
|
||||
}
|
||||
|
||||
class Bar {
|
||||
func doSomething(context: Context) {
|
||||
var c = context // context is a value type, changes only effect this instance
|
||||
c["bar"] = "baz"
|
||||
c.logger.info("\(self)::doSomething")
|
||||
c.logger.trace("\(self)::doSomething:someDebugInfo")
|
||||
Baz().doSomething(context: c)
|
||||
c.logger.info("\(self)::doSomething::end")
|
||||
}
|
||||
}
|
||||
|
||||
class Baz {
|
||||
private let queue = DispatchQueue(label: "ContextBasedSystem::Baz")
|
||||
|
||||
func doSomething(context: Context) {
|
||||
context.logger.info("\(self)::doSomething")
|
||||
context.logger.trace("\(self)::doSomething:someDebugInfo")
|
||||
let group = DispatchGroup()
|
||||
group.enter()
|
||||
queue.async {
|
||||
context.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 = context.logger // logger is a value type, changes only effect this instance
|
||||
l[metadataKey: "baz"] = "qux"
|
||||
l.info("\(self)::doSomething::Local")
|
||||
context.logger.info("\(self)::doSomething::end")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user