mirror of
https://github.com/apple/swift-log
synced 2026-11-29 04:14:22 +00:00
naming proposal (#2)
* naming proposal motivation: intuitive APIs changes: * Logger is the main API users interact with * Logging is the factory from which users get instances of Logger, via Logging.for(identifer) * LogHandler is what logging libraries implement, this is not exposed to users as its boxed in Logger * replace Logging (aka LoggerFactory) factory property with bootstrap(factory) function * rename main module to Logging since its not server only * converge examples into an Examples module open questions: * Logging.make or Logging.for * is Logging.for(object: Any.Type) a good idea? is there a better way to get the FQN for an object?
This commit is contained in:
1 parent
a4cb92d526
commit
b5dcb3140e
12 files changed
+80
-91
No files matched your search
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER IMPLEMENTATION LOOKS LIKE
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Logging
|
||||
|
||||
private extension NSLock {
|
||||
func withLock<T>(_ body: () -> T) -> T {
|
||||
self.lock()
|
||||
defer {
|
||||
self.unlock()
|
||||
}
|
||||
return body()
|
||||
}
|
||||
}
|
||||
|
||||
public final class ExampleLoggerImplementation: LogHandler {
|
||||
private let formatter: DateFormatter
|
||||
private let label: String
|
||||
private let lock = NSLock()
|
||||
|
||||
private var _logLevel: LogLevel = .info
|
||||
public var logLevel: LogLevel {
|
||||
get {
|
||||
return self.lock.withLock { self._logLevel }
|
||||
}
|
||||
set {
|
||||
self.lock.withLock {
|
||||
self._logLevel = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 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)")
|
||||
}
|
||||
|
||||
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(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