Files
swift-log/Sources/ExampleImplementation/SimpleLogging.swift
T
tomer doron 19d987b6d5 add logger API variants that take an error (#12)
motivation: easily log errors w/o converting them to string first

changes:
* add another set of logger APIs that take in an error object
* add tests and refactor testing assertions
* add autoclosure tests to make sure its resoved on when log level is appropriate
2019-01-08 20:41:03 +00:00

49 lines
1.6 KiB
Swift

//
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER IMPLEMENTATION LOOKS LIKE
//
import Foundation
import Logging
// this is a contrived example of a logging library implementation that writes logs to stdout
public final class SimpleLogging {
private let defaultLogLevel = Logging.Level.info
public init() {}
public func make(label: String) -> LogHandler {
return Logger(label: label, defaultLogLevel: self.defaultLogLevel)
}
private struct Logger: LogHandler {
private var logger: SimpleLogger
private let defaultLogLevel: Logging.Level
public init(label: String, defaultLogLevel: Logging.Level) {
self.logger = SimpleLogger(label: label)
self.defaultLogLevel = defaultLogLevel
}
public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) {
self.logger.log(level: level, message: message, error: error) { text in
print(text)
}
}
public var logLevel: Logging.Level {
get { return self.logger.logLevel ?? self.defaultLogLevel }
set { self.logger.logLevel = newValue }
}
public var metadata: Logging.Metadata? {
get { return self.logger.metadata }
set { self.logger.metadata = newValue }
}
public subscript(metadataKey metadataKey: String) -> Logging.Metadata.Value? {
get { return self.logger[metadataKey: metadataKey] }
set { self.logger[metadataKey: metadataKey] = newValue }
}
}
}