Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e18692fd4d | ||
|
|
b48562c2b0 | ||
|
|
d22556d04e | ||
|
|
e3f2ba03ef |
@@ -66,6 +66,13 @@ func init() {
|
||||
"main": Logger(label: "com.example.yourapp.Main"),
|
||||
"mail": Logger(label: "com.example.yourapp.Mail System"),
|
||||
]
|
||||
|
||||
// And using loggers
|
||||
logger["main"]!.debug("Starting build",
|
||||
metadata: [
|
||||
"input.output_directory": "\(output)",
|
||||
"input.data": "\(data ?? "-")"
|
||||
])
|
||||
}
|
||||
```
|
||||
|
||||
@@ -78,6 +85,9 @@ library maps a reverse domain style label to the subsystem and category
|
||||
parameters of the unified logging system. See
|
||||
``/LoggingOSLog/LoggingOSLog/init(label:)`` for more information.
|
||||
|
||||
Metadata is appended as JSON to the logged message after a simple unicode right
|
||||
arrow ("→").
|
||||
|
||||
### What this library does not supply
|
||||
This library does not implement any of the interpolation privacy features of
|
||||
the unified logging system, i.e. there is no way to redact or align a meta
|
||||
|
||||
41
Sources/Logging OSLog/Logger Metadata Extension.swift
Normal file
41
Sources/Logging OSLog/Logger Metadata Extension.swift
Normal file
@@ -0,0 +1,41 @@
|
||||
import Logging
|
||||
|
||||
extension Logging.Logger.Metadata {
|
||||
public func asJSON() -> String {
|
||||
return Self.asJSON(self)
|
||||
}
|
||||
|
||||
private static func asJSON(_ metadata: Logging.Logger.Metadata) -> String {
|
||||
var outputParts: [String: String] = [:]
|
||||
for (key, value) in metadata {
|
||||
let jsonKey = Self.escapeForJSON(key)
|
||||
outputParts[jsonKey] = Self.asJSON(value)
|
||||
}
|
||||
return "{" + outputParts.map { "\"\($0)\": \($1)" }.joined(separator: ", ") + "}"
|
||||
}
|
||||
|
||||
private static func asJSON(_ metadata: [Logging.Logger.MetadataValue]) -> String {
|
||||
var outputParts: [String] = []
|
||||
for item in metadata {
|
||||
outputParts.append(Self.asJSON(item))
|
||||
}
|
||||
return "[" + outputParts.joined(separator: ", ") + "]"
|
||||
}
|
||||
|
||||
private static func asJSON(_ metadata: Logging.Logger.MetadataValue) -> String {
|
||||
switch metadata {
|
||||
case .dictionary(let subvalues):
|
||||
return Self.asJSON(subvalues)
|
||||
case .array(let subvalues):
|
||||
return Self.asJSON(subvalues)
|
||||
case .string(let subvalue):
|
||||
return "\"" + Self.escapeForJSON(subvalue) + "\""
|
||||
case .stringConvertible(let subvalue):
|
||||
return "\"" + Self.escapeForJSON("\(subvalue)") + "\""
|
||||
}
|
||||
}
|
||||
|
||||
private static func escapeForJSON(_ data: String) -> String {
|
||||
return data.replacingOccurrences(of: "\"", with: "\\\"")
|
||||
}
|
||||
}
|
||||
@@ -78,10 +78,10 @@ public struct LoggingOSLog: LogHandler {
|
||||
function: String,
|
||||
line: UInt
|
||||
) {
|
||||
let metadataCSV = Self.joinMetadata(self.metadata, self.metadataProvider?.get(), metadata)
|
||||
let metadataCSV = Self.joinedMetadata(self.metadata, self.metadataProvider?.get(), metadata)
|
||||
let messageParts = [message.description, metadataCSV]
|
||||
|
||||
let message = messageParts.compactMap { $0 }.joined(separator: " -> ")
|
||||
let message = messageParts.compactMap { $0 }.joined(separator: " → ")
|
||||
self.oslogger.log(level: OSLogType.from(loggerLevel: level), "\(message, privacy: .public)")
|
||||
}
|
||||
|
||||
@@ -97,17 +97,12 @@ public struct LoggingOSLog: LogHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static func joinMetadata(_ metadataList: Logging.Logger.Metadata?...) -> String? {
|
||||
private static func joinedMetadata(_ metadataList: Logging.Logger.Metadata?...) -> String? {
|
||||
var metadataAggregator: Logging.Logger.Metadata = [:]
|
||||
for metadata in metadataList {
|
||||
guard let metadata = metadata else { continue }
|
||||
metadataAggregator.merge(metadata) { return $1 }
|
||||
}
|
||||
return Self.joinMetadata(metadataAggregator)
|
||||
}
|
||||
|
||||
private static func joinMetadata(_ metadata: Logging.Logger.Metadata, with separator: String = ", ") -> String? {
|
||||
guard !metadata.isEmpty else { return nil }
|
||||
return metadata.map { "\($0) = \($1)" }.joined(separator: separator)
|
||||
return metadataAggregator.asJSON()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,16 @@ import LoggingOSLog
|
||||
@Test func canLogMessage() {
|
||||
let logger = Logging.Logger(label: "de.astzweig.loggingoslog.Test")
|
||||
logger.info("Test message")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func canLogMessageWithMetadata() {
|
||||
let logger = Logging.Logger(label: "de.astzweig.loggingoslog.Test")
|
||||
logger.info("Test message", metadata: [
|
||||
"request.id": "20140801",
|
||||
"request.dirname": "\"Impossible\"",
|
||||
"request.authorization": [
|
||||
"bearer": "empty"
|
||||
],
|
||||
"request.values": ["1", "rootID", ["key": "value"]]
|
||||
])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user