mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
logger metadata support and unit tests
motivation: add tests to confirm desired functionality changes: * add property for logger metadata (aka context) * improve pretty-metadata (aka pretty-context) generation * add unit tests covering local and global use cases * add [test only] implementation of MDC * format with swiftformat open issues: * should we name the new property metadata or context * should we include a default MDC implmentation as part of the logging API. if we dont, we will encourage libraries binding to specific logging implementations that support it (that was bad in java) * tests are a bit brittle * why is ExampleLoggerImplementation a reference type?
This commit is contained in:
1 parent
d3fc8c10e4
commit
bf76e03ea3
12 files changed
+723
-83
No files matched your search
@@ -18,18 +18,8 @@ private extension NSLock {
|
||||
public final class ExampleLoggerImplementation: LogEmitter {
|
||||
private let formatter: DateFormatter
|
||||
private let identifier: String
|
||||
|
||||
private let lock = NSLock()
|
||||
private var context: [String: String] = [:] {
|
||||
didSet {
|
||||
if self.context.isEmpty {
|
||||
self.prettyContext = ""
|
||||
} else {
|
||||
self.prettyContext = " \(self.context.description) @\(self.identifier)"
|
||||
}
|
||||
}
|
||||
}
|
||||
private var prettyContext: String
|
||||
|
||||
private var _logLevel: LogLevel = .info
|
||||
public var logLevel: LogLevel {
|
||||
get {
|
||||
@@ -41,9 +31,8 @@ public final class ExampleLoggerImplementation: LogEmitter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public init(identifier: String) {
|
||||
self.prettyContext = ""
|
||||
self.identifier = identifier
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
||||
@@ -51,7 +40,7 @@ public final class ExampleLoggerImplementation: LogEmitter {
|
||||
formatter.calendar = Calendar(identifier: .gregorian)
|
||||
self.formatter = formatter
|
||||
}
|
||||
|
||||
|
||||
private func formatLevel(_ level: LogLevel) -> String {
|
||||
switch level {
|
||||
case .error:
|
||||
@@ -66,17 +55,38 @@ public final class ExampleLoggerImplementation: LogEmitter {
|
||||
return "trce"
|
||||
}
|
||||
}
|
||||
|
||||
public func log(level: LogLevel, message: String, file: String, function: String, line: UInt) {
|
||||
print("\(self.formatter.string(from: Date())) \(self.formatLevel(level)): \(message)\(self.prettyContext)")
|
||||
|
||||
public func log(level: LogLevel, message: String, file _: String, function _: String, line _: UInt) {
|
||||
print("\(self.formatter.string(from: Date()))\(self.prettyMetadata.map { " \($0)" } ?? "") \(self.formatLevel(level)): \(message)")
|
||||
}
|
||||
|
||||
public subscript(diagnosticKey diagnosticKey: String) -> String? {
|
||||
|
||||
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.context[diagnosticKey] }
|
||||
return self.lock.withLock { self._metadata }
|
||||
}
|
||||
set {
|
||||
self.lock.withLock { self.context[diagnosticKey] = newValue }
|
||||
self.lock.withLock { self._metadata = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
public subscript(diagnosticKey diagnosticKey: String) -> String? {
|
||||
get {
|
||||
return self.lock.withLock { self._metadata?[diagnosticKey] }
|
||||
}
|
||||
set {
|
||||
self.lock.withLock {
|
||||
if nil == self._metadata {
|
||||
self._metadata = [:]
|
||||
}
|
||||
self._metadata![diagnosticKey] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user