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
79 lines
2.1 KiB
Swift
79 lines
2.1 KiB
Swift
//
|
|
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER IMPLEMENTATION LOOKS LIKE
|
|
//
|
|
|
|
import Foundation
|
|
import Logging
|
|
|
|
// helper class to keep things DRY
|
|
internal final class SimpleLogger {
|
|
let label: String
|
|
private var _logLevel: LogLevel?
|
|
private let formatter: DateFormatter
|
|
private let lock = NSLock()
|
|
|
|
public init(label: String) {
|
|
self.label = label
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
|
formatter.locale = Locale(identifier: "en_US")
|
|
formatter.calendar = Calendar(identifier: .gregorian)
|
|
self.formatter = formatter
|
|
}
|
|
|
|
public func log(level: LogLevel, message: String, printer: (String) -> Void) {
|
|
printer("[\(self.label)] \(self.formatter.string(from: Date()))\(self.prettyMetadata.map { " \($0)" } ?? "") \(level): \(message)")
|
|
}
|
|
|
|
public var logLevel: LogLevel? {
|
|
get {
|
|
return self.lock.withLock { self._logLevel }
|
|
}
|
|
set {
|
|
self.lock.withLock {
|
|
self._logLevel = newValue
|
|
}
|
|
}
|
|
}
|
|
|
|
private var prettyMetadata: String?
|
|
private var _metadata: LoggingMetadata? {
|
|
didSet {
|
|
self.prettyMetadata = !(self._metadata?.isEmpty ?? true) ? self._metadata!.map { "\($0)=\($1)" }.joined(separator: " ") : nil
|
|
}
|
|
}
|
|
|
|
public var metadata: LoggingMetadata? {
|
|
get {
|
|
return self.lock.withLock { self._metadata }
|
|
}
|
|
set {
|
|
self.lock.withLock { self._metadata = newValue }
|
|
}
|
|
}
|
|
|
|
public subscript(metadataKey metadataKey: String) -> String? {
|
|
get {
|
|
return self.lock.withLock { self._metadata?[metadataKey] }
|
|
}
|
|
set {
|
|
self.lock.withLock {
|
|
if nil == self._metadata {
|
|
self._metadata = [:]
|
|
}
|
|
self._metadata![metadataKey] = newValue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension NSLock {
|
|
func withLock<T>(_ body: () -> T) -> T {
|
|
self.lock()
|
|
defer {
|
|
self.unlock()
|
|
}
|
|
return body()
|
|
}
|
|
}
|