mirror of
https://github.com/apple/swift-log
synced 2026-11-25 03:57:48 +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
@@ -1,6 +1,6 @@
|
||||
# swift-server-logger-api
|
||||
# SSWG Logger Proposal
|
||||
|
||||
This is *pre-pitch stage* SSWG proposal about a logger API that intends to support a number programming models:
|
||||
This is SSWG proposal about a logger API that intends to support a number programming models:
|
||||
|
||||
- explicit logger passing (see `ExplicitLoggerPassingExample.swift`)
|
||||
- one global logger (see `OneGlobalLoggerExample.swift`)
|
||||
@@ -11,6 +11,7 @@ The file `RandomExample.swift` contains demoes of some other things that you may
|
||||
## Feedback Wishes
|
||||
|
||||
Feedback that would really be great is:
|
||||
|
||||
- if anything, what does this proposal *not cover* that you will definitely need
|
||||
- if anything, what could we remove from this and still be happy?
|
||||
- API-wise: what do you like, what don't you like?
|
||||
@@ -21,7 +22,7 @@ Feel free to post this as message on the SSWG forum and/or github issues in this
|
||||
|
||||
Very many. But here a couple that come to my mind:
|
||||
|
||||
- currently attaching metadata to a logger is done through `subscript(diagnosticKey diagnosticKey: String) -> String? { get set }`
|
||||
- currently attaching metadata to a logger is done through `subscript(metadataKey metadataKey: String) -> String? { get set }`
|
||||
clearly setting and deleting extra metadata is necessary. But reading it is really not. Should we make it `addContext(key: String, value: String)` and `removeContext(key:String)` instead?
|
||||
- should the logging metadata values be `String`?
|
||||
- should this library include an [MDC](https://logback.qos.ch/manual/mdc.html) API? should it be a seperate module? or a seperate library? [SLF4J](https://www.slf4j.org/manual.html#mdc) which is the moral equivilant of this API in the JVM ecosystem does include one
|
||||
@@ -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")
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
import Foundation
|
||||
|
||||
/// This is the protocol a custom logger implements.
|
||||
public protocol LogHandler {
|
||||
/// not called directly, only by the helper methods like `info(...)`
|
||||
// This is the custom logger implementation's log function. A user would not invoke this but rather go through
|
||||
// `Logger`'s `info`, `error`, or `warning` functions.
|
||||
//
|
||||
// An implementation does not need to check the log level because that has been done before by `Logger` itself.
|
||||
func log(level: LogLevel, message: String, file: String, function: String, line: UInt)
|
||||
|
||||
/// This adds diagnostic context to a place the concrete logger considers appropriate. Some loggers
|
||||
/// might not support this feature at all.
|
||||
subscript(diagnosticKey _: LoggingMetadata.Key) -> LoggingMetadata.Value? { get set }
|
||||
// This adds metadata to a place the concrete logger considers appropriate. Some loggers
|
||||
// might not support this feature at all.
|
||||
subscript(metadataKey _: LoggingMetadata.Key) -> LoggingMetadata.Value? { get set }
|
||||
|
||||
// All available metatdata
|
||||
var metadata: LoggingMetadata? { get set }
|
||||
|
||||
// The log level
|
||||
var logLevel: LogLevel { get set }
|
||||
}
|
||||
|
||||
// This is the logger itself. It can either have value or reference semantics, depending on the `LogHandler`
|
||||
// implementation.
|
||||
public struct Logger {
|
||||
@usableFromInline
|
||||
var handler: LogHandler
|
||||
@@ -54,12 +62,12 @@ public struct Logger {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(diagnosticKey diagnosticKey: String) -> String? {
|
||||
public subscript(metadataKey metadataKey: String) -> String? {
|
||||
get {
|
||||
return self.handler[diagnosticKey: diagnosticKey]
|
||||
return self.handler[metadataKey: metadataKey]
|
||||
}
|
||||
set {
|
||||
self.handler[diagnosticKey: diagnosticKey] = newValue
|
||||
self.handler[metadataKey: metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,22 +108,19 @@ extension LogLevel: Comparable {
|
||||
}
|
||||
}
|
||||
|
||||
/// The second most important type, this is where users will get a logger from.
|
||||
// This is the logging system itself, it's mostly used to obtain loggers and to set the type of the `LogHandler`
|
||||
// implementation.
|
||||
public enum Logging {
|
||||
private static let lock = NSLock()
|
||||
private static var _factory: (String) -> LogHandler = StdoutLogger.init
|
||||
|
||||
// Configures which `LogHandler` to use in the application.
|
||||
public static func bootstrap(_ factory: @escaping (String) -> LogHandler) {
|
||||
self.lock.withLock {
|
||||
self._factory = factory
|
||||
}
|
||||
}
|
||||
|
||||
// this is used to create a logger for a certain unit which might be a module, file, class/struct, function, whatever works for the concrete application. Systems that pass the logger explicitly would not use this function.
|
||||
public static func make(for object: Any.Type) -> Logger {
|
||||
return self.make(String(describing: object))
|
||||
}
|
||||
|
||||
public static func make(_ label: String) -> Logger {
|
||||
return self.lock.withLock { Logger(self._factory(label)) }
|
||||
}
|
||||
@@ -161,16 +166,16 @@ public final class StdoutLogger: 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ private struct Struct3 {
|
||||
MDC.global["foo"] = nil
|
||||
// only effects the logger instance
|
||||
var l = logger
|
||||
l[diagnosticKey: "baz"] = "qux"
|
||||
l[metadataKey: "baz"] = "qux"
|
||||
l.trace("Struct3::doSomethingElse::Local")
|
||||
logger.trace("Struct3::doSomethingElse::end")
|
||||
}
|
||||
|
||||
@@ -68,9 +68,9 @@ private struct Context {
|
||||
}
|
||||
|
||||
// since logger is a value type, we can reuse our copy to manage metadata
|
||||
subscript(diagnosticKey: String) -> String? {
|
||||
get { return self.logger[diagnosticKey: diagnosticKey] }
|
||||
set { self.logger[diagnosticKey: diagnosticKey] = newValue }
|
||||
subscript(metadataKey: String) -> String? {
|
||||
get { return self.logger[metadataKey: metadataKey] }
|
||||
set { self.logger[metadataKey: metadataKey] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ private struct Struct3 {
|
||||
group.wait()
|
||||
// only effects the logger instance
|
||||
var l = context.logger
|
||||
l[diagnosticKey: "baz"] = "qux"
|
||||
l[metadataKey: "baz"] = "qux"
|
||||
l.trace("Struct3::doSomethingElse::Local")
|
||||
context.logger.trace("Struct3::doSomethingElse::end")
|
||||
}
|
||||
|
||||
@@ -51,18 +51,18 @@ internal struct TestLoggger: LogHandler {
|
||||
|
||||
// TODO: would be nice to deleagte to local copy of logger but StdoutLogger is a reference type. why?
|
||||
private var _metadata: LoggingMetadata?
|
||||
subscript(diagnosticKey diagnosticKey: LoggingMetadata.Key) -> LoggingMetadata.Value? {
|
||||
subscript(metadataKey metadataKey: LoggingMetadata.Key) -> LoggingMetadata.Value? {
|
||||
get {
|
||||
// return self.logger[diagnosticKey: diagnosticKey]
|
||||
return self.metadataLock.withLock { self._metadata?[diagnosticKey] }
|
||||
// return self.logger[metadataKey: metadataKey]
|
||||
return self.metadataLock.withLock { self._metadata?[metadataKey] }
|
||||
}
|
||||
set {
|
||||
// return logger[diagnosticKey: diagnosticKey] = newValue
|
||||
// return logger[metadataKey: metadataKey] = newValue
|
||||
self.metadataLock.withLock {
|
||||
if nil == self._metadata {
|
||||
self._metadata = LoggingMetadata()
|
||||
}
|
||||
self._metadata![diagnosticKey] = newValue
|
||||
self._metadata![metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,10 +184,10 @@ public class MDC {
|
||||
|
||||
private init() {}
|
||||
|
||||
public subscript(diagnosticKey: LoggingMetadata.Key) -> LoggingMetadata.Value? {
|
||||
public subscript(metadataKey: LoggingMetadata.Key) -> LoggingMetadata.Value? {
|
||||
get {
|
||||
return self.lock.withLock {
|
||||
self.storage[self.threadId]?[diagnosticKey]
|
||||
self.storage[self.threadId]?[metadataKey]
|
||||
}
|
||||
}
|
||||
set {
|
||||
@@ -195,7 +195,7 @@ public class MDC {
|
||||
if nil == self.storage[self.threadId] {
|
||||
self.storage[self.threadId] = LoggingMetadata()
|
||||
}
|
||||
self.storage[self.threadId]![diagnosticKey] = newValue
|
||||
self.storage[self.threadId]![metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user