mirror of
https://github.com/apple/swift-log
synced 2026-11-27 04:14:20 +00:00
use the term "metadata"
This commit is contained in:
1 parent
c7c54cc237
commit
50a8c8fdac
10 files changed
+102
-43
No files matched your search
@@ -15,6 +15,56 @@ private extension NSLock {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ExampleValueLoggerImplementation: LogHandler {
|
||||
private var _metadata: LoggingMetadata = [:]
|
||||
|
||||
public init(label: String) {
|
||||
}
|
||||
|
||||
public func log(level: LogLevel, message: String, file: String, function: String, line: UInt) {
|
||||
print("\(self.formatLevel(level)): \(message) \(self.metadata?.description ?? "")")
|
||||
}
|
||||
|
||||
private func formatLevel(_ level: LogLevel) -> String {
|
||||
switch level {
|
||||
case .error:
|
||||
return "ERRO"
|
||||
case .warn:
|
||||
return "WARN"
|
||||
case .info:
|
||||
return "info"
|
||||
case .debug:
|
||||
return "dbug"
|
||||
case .trace:
|
||||
return "trce"
|
||||
}
|
||||
}
|
||||
|
||||
public subscript(metadataKey metadataKey: LoggingMetadata.Key) -> LoggingMetadata.Value? {
|
||||
get {
|
||||
return self._metadata[metadataKey]
|
||||
}
|
||||
set(newValue) {
|
||||
self._metadata[metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
public var metadata: LoggingMetadata? {
|
||||
get {
|
||||
return self._metadata
|
||||
}
|
||||
set {
|
||||
if let newValue = newValue {
|
||||
self._metadata = newValue
|
||||
} else {
|
||||
self._metadata.removeAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public var logLevel: LogLevel = .info
|
||||
}
|
||||
|
||||
public final class ExampleLoggerImplementation: LogHandler {
|
||||
private let formatter: DateFormatter
|
||||
private let label: String
|
||||
@@ -76,16 +126,16 @@ public final class ExampleLoggerImplementation: LogHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public subscript(diagnosticKey diagnosticKey: String) -> String? {
|
||||
public subscript(metadataKey metadataKey: String) -> String? {
|
||||
get {
|
||||
return self.lock.withLock { self._metadata?[diagnosticKey] }
|
||||
return self.lock.withLock { self._metadata?[metadataKey] }
|
||||
}
|
||||
set {
|
||||
self.lock.withLock {
|
||||
if nil == self._metadata {
|
||||
self._metadata = [:]
|
||||
}
|
||||
self._metadata![diagnosticKey] = newValue
|
||||
self._metadata![metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import Logging
|
||||
private class Client {
|
||||
func spawnFreshClient(uuid: UUID, logger: Logger) {
|
||||
var logger = logger
|
||||
logger[diagnosticKey: "UUID"] = uuid.description
|
||||
logger[metadataKey: "UUID"] = uuid.description
|
||||
logger.info("we're up and running")
|
||||
let otherSubSystem = OtherSubSystem()
|
||||
otherSubSystem.randomFunction(logger: logger)
|
||||
@@ -20,6 +20,7 @@ private class OtherSubSystem {
|
||||
|
||||
enum ExplicitContextPassingExample {
|
||||
static func main() {
|
||||
Logging.bootstrap(ExampleValueLoggerImplementation.init)
|
||||
let logger = Logging.make("com.example.TestApp.ExplicitContextPassingExample.main")
|
||||
for clientID in 0 ..< 10 {
|
||||
var clientLogger = logger
|
||||
|
||||
@@ -8,7 +8,7 @@ private class Client {
|
||||
var logger = Logging.make("com.example.TestApp.LoggerPerSubsystemExample.Client")
|
||||
// NOTE: the log level change below will only affect this `Client` instance which might or might not be what you want.
|
||||
logger.logLevel = logLevel
|
||||
logger[diagnosticKey: "UUID"] = uuid.description
|
||||
logger[metadataKey: "UUID"] = uuid.description
|
||||
self.logger = logger
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ private class OtherSubSystem {
|
||||
|
||||
enum LoggerPerSubsystemExample {
|
||||
static func main() {
|
||||
Logging.bootstrap(ExampleLoggerImplementation.init)
|
||||
let logger = Logging.make("com.example.TestApp.LoggerPerSubsystemExample.main")
|
||||
|
||||
logger.info("main start")
|
||||
|
||||
@@ -7,6 +7,7 @@ let logger = Logging.make("com.example.TestApp.OneGlobalLoggerExample")
|
||||
|
||||
enum OneGlobalLoggerExample {
|
||||
static func main() {
|
||||
Logging.bootstrap(ExampleLoggerImplementation.init)
|
||||
for _ in 0 ..< 10 {
|
||||
// NOTE: we can't change the log level per client because we only have one global logger
|
||||
self.spawnFreshClient(uuid: UUID())
|
||||
|
||||
@@ -12,8 +12,8 @@ enum RandomExample {
|
||||
logger.warn("default, warn, no context")
|
||||
logger.error("default, error, no context")
|
||||
|
||||
logger[diagnosticKey: "foo"] = "bar"
|
||||
logger[diagnosticKey: "UUID"] = UUID().description
|
||||
logger[metadataKey: "foo"] = "bar"
|
||||
logger[metadataKey: "UUID"] = UUID().description
|
||||
|
||||
logger.trace("trace, not logged")
|
||||
logger.debug("debug, not logged")
|
||||
@@ -35,8 +35,8 @@ enum RandomExample {
|
||||
logger.warn("warn, no context")
|
||||
logger.error("error, no context")
|
||||
|
||||
logger[diagnosticKey: "bar"] = "buz"
|
||||
logger[diagnosticKey: "UUID"] = UUID().description
|
||||
logger[metadataKey: "bar"] = "buz"
|
||||
logger[metadataKey: "UUID"] = UUID().description
|
||||
|
||||
logger.trace("trace, not logged")
|
||||
logger.debug("debug, not logged")
|
||||
@@ -54,8 +54,8 @@ enum RandomExample {
|
||||
logger.warn("warn, no context")
|
||||
logger.error("error, no context")
|
||||
|
||||
logger[diagnosticKey: "buz"] = "cux"
|
||||
logger[diagnosticKey: "UUID"] = UUID().description
|
||||
logger[metadataKey: "buz"] = "cux"
|
||||
logger[metadataKey: "UUID"] = UUID().description
|
||||
|
||||
logger.trace("trace, not logged")
|
||||
logger.debug("debug, not logged")
|
||||
|
||||
Reference in new issue
Block a user