mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
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
49 lines
1.6 KiB
Swift
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 }
|
|
}
|
|
}
|
|
}
|