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:
tomer doron committed 2019-01-05 02:03:44 -08:00
1 parent 0f23cda751
commit 9027df5f03
25 files changed
+858 -381

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER USAGE LOOKS LIKE
//
import ExampleImplementation
import Foundation
import Logging
enum ConfigExample {
static func main() {
// boostrap with our config based sample implementations
let logging = ConfigLogging()
Logging.bootstrap(logging.make)
// run the example
let logger = Logging.make("com.example.TestApp")
logger.trace("hello world?")
// changes the config in runtime, real implemnetations will use config files instead of in-memory config
logging.config.set(key: "com.example.TestApp", value: .trace)
logger.trace("hello world!")
}
}
@@ -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")
}
}
}
@@ -0,0 +1,82 @@
//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER USAGE LOOKS LIKE
//
import ExampleImplementation
import Foundation
import Logging
var logger = Logging.make("GlobalLogger")
// this is a contrived example of a system that shares one global logger
enum GlobalLoggerBasedSystem {
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) ----------------------------")
logger[metadataKey: "requestId"] = UUID().uuidString
Foo().doSomething(requestNumnber: i)
}
}
class Foo {
func doSomething(requestNumnber: Int) {
if 0 == requestNumnber % 2 {
// debug verbosely every 2nd request
logger.logLevel = .trace
}
logger.info("\(self)::doSomething")
self.doSomethingElse()
logger.info("\(self)::doSomething::end")
}
private func doSomethingElse() {
logger[metadataKey: "foo"] = "bar"
logger.info("\(self)::doSomethingElse")
logger.trace("\(self)::doSomethingElse:someDebugInfo")
Bar().doSomething()
logger.info("\(self)::doSomethingElse::end")
logger[metadataKey: "foo"] = nil
}
}
class Bar {
func doSomething() {
logger[metadataKey: "bar"] = "baz"
logger.info("\(self)::doSomething")
logger.trace("\(self)::doSomething:someDebugInfo")
Baz().doSomething()
logger.info("\(self)::doSomething::end")
logger[metadataKey: "bar"] = nil
}
}
class Baz {
private let queue = DispatchQueue(label: "GlobalLoggerBasedSystem::Baz")
func doSomething() {
logger.info("\(self)::doSomething")
logger.trace("\(self)::doSomething:someDebugInfo")
let group = DispatchGroup()
group.enter()
queue.async {
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")
}
}
}
@@ -0,0 +1,90 @@
//
// 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")
}
}
}
+18
View File
@@ -0,0 +1,18 @@
//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER USAGE LOOKS LIKE
//
import ExampleImplementation
import Foundation
import Logging
enum MUXExample {
static func main() {
// boostrap with two of our sample implementations
let logging = MultiplexLogging([SimpleLogging().make, FileLogging().make])
Logging.bootstrap(logging.make)
// run the example
let logger = Logging.make("com.example.TestApp")
logger.info("hello world!")
}
}
+27
View File
@@ -0,0 +1,27 @@
//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER USAGE LOOKS LIKE
//
import Foundation
import Logging
// this is a contrived example of a 3rd party library that is used by the other examples
// this library does not know anything about the systewm that used it, so cannot make
// assumptions about them
class RandomLibrary {
private let logger = Logging.make("RandomLibrary")
private let queue = DispatchQueue(label: "RandomLibrary")
public init() {}
public func doSomething() {
self.logger.info("doSomething")
}
public func doSomethingAsync(completion: @escaping () -> Void) {
self.queue.asyncAfter(deadline: .now() + 0.1) {
self.logger.info("doSomethingAsync")
completion()
}
}
}
+19
View File
@@ -0,0 +1,19 @@
print()
print("##### global logger based system #####")
GlobalLoggerBasedSystem.main()
print()
print("##### local logger based system #####")
LocalLoggerBasedSystem.main()
print()
print("##### context based system #####")
ContextBasedSystem.main()
print()
print("##### multiplexiing to multiple logging implementations #####")
MUXExample.main()
print()
print("##### config based logging implementations #####")
ConfigExample.main()