Files
swift-log/Sources/ExampleImplementation/SimpleLogHandler.swift
T
tomer doron 045ba36718 remove error argument from logging API (#34)
motivation: since Error in swift does not carry any additional information (such as stack traces) that can be useful in formatting decision the LogHandler may need to do, there is no real reason to treat it as special in the API, and it can just be "strigified" at the calling site

changes: remove error params from all APIs
2019-02-22 18:02:14 +00:00

43 lines
1.4 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 struct SimpleLogHandler: LogHandler {
private var handler: CommonLogHandler
private let defaultLogLevel: Logger.Level
public init(label: String) {
self.init(label: label, defaultLogLevel: .info)
}
public init(label: String, defaultLogLevel: Logger.Level) {
self.handler = CommonLogHandler(label: label)
self.defaultLogLevel = defaultLogLevel
}
public func log(level: Logger.Level, message: String, metadata: Logger.Metadata?, file: StaticString, function: StaticString, line: UInt) {
self.handler.log(level: level, message: message, metadata: metadata) { text in
print(text)
}
}
public var logLevel: Logger.Level {
get { return self.handler.logLevel ?? self.defaultLogLevel }
set { self.handler.logLevel = newValue }
}
public var metadata: Logger.Metadata {
get { return self.handler.metadata }
set { self.handler.metadata = newValue }
}
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get { return self.handler[metadataKey: metadataKey] }
set { self.handler[metadataKey: metadataKey] = newValue }
}
}