mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
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
49 lines
1.5 KiB
Swift
49 lines
1.5 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 = LogLevel.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: LogLevel
|
|
|
|
public init(label: String, defaultLogLevel: LogLevel) {
|
|
self.logger = SimpleLogger(label: label)
|
|
self.defaultLogLevel = defaultLogLevel
|
|
}
|
|
|
|
public func log(level: LogLevel, message: String, file _: String, function _: String, line _: UInt) {
|
|
self.logger.log(level: level, message: message) { text in
|
|
print(text)
|
|
}
|
|
}
|
|
|
|
public var logLevel: LogLevel {
|
|
get { return self.logger.logLevel ?? self.defaultLogLevel }
|
|
set { self.logger.logLevel = newValue }
|
|
}
|
|
|
|
public var metadata: LoggingMetadata? {
|
|
get { return self.logger.metadata }
|
|
set { self.logger.metadata = newValue }
|
|
}
|
|
|
|
public subscript(metadataKey metadataKey: String) -> String? {
|
|
get { return self.logger[metadataKey: metadataKey] }
|
|
set { self.logger[metadataKey: metadataKey] = newValue }
|
|
}
|
|
}
|
|
}
|