mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
motivation: allow users to define metadata at the log API call site changes: add metadata param to all log APIs, and implement merge at our example implementations
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, metadata: Logging.Metadata?, error: Error?, file: StaticString, function: StaticString, line: UInt) {
|
|
self.logger.log(level: level, message: message, metadata: metadata, 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 }
|
|
}
|
|
}
|
|
}
|