-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8f19d0f
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// swift-tools-version:5.5 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "LoggingPlayground", | ||
products: [ | ||
.library( | ||
name: "LoggingPlayground", | ||
targets: ["LoggingPlayground"] | ||
) | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "LoggingPlayground", | ||
dependencies: [] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# LoggingPlayground | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Foundation | ||
import Logging | ||
|
||
public struct PlaygroundHandler { | ||
private let label: String | ||
private var prettyMetadata: String? | ||
public var metadata = Logger.Metadata() { | ||
didSet { | ||
self.prettyMetadata = self.prettify(self.metadata) | ||
} | ||
} | ||
public var logLevel = Logger.Level.info | ||
|
||
public init(label: String) { | ||
self.label = label | ||
} | ||
|
||
private func prettify(_ metadata: Logger.Metadata) -> String? { | ||
guard !metadata.isEmpty else { return nil } | ||
return metadata.lazy | ||
.sorted(by: { $0.key < $1.key }) | ||
.map { "\($0)=\($1)" } | ||
.joined(separator: " ") | ||
} | ||
|
||
private func timestamp() -> String { | ||
var buffer = [Int8](repeating: 0, count: 255) | ||
var timestamp = time(nil) | ||
let localTime = localtime(×tamp) | ||
strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime) | ||
return buffer.withUnsafeBufferPointer { | ||
$0.withMemoryRebound(to: CChar.self) { | ||
String(cString: $0.baseAddress!) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension PlaygroundHandler: LogHandler { | ||
public func log( | ||
level: Logger.Level, | ||
message: Logger.Message, | ||
metadata: Logger.Metadata?, | ||
source: String, | ||
file: String, | ||
function: String, | ||
line: UInt | ||
) { | ||
let prettyMetadata: String? | ||
if let metadata = metadata, !metadata.isEmpty { | ||
prettyMetadata = self.prettify( | ||
self.metadata.merging( | ||
metadata, | ||
uniquingKeysWith: { _, new in new } | ||
) | ||
) | ||
} else { | ||
prettyMetadata = self.prettyMetadata | ||
} | ||
print( | ||
"\(self.timestamp()) [\(self.label)] \(level):\(prettyMetadata.map { " \($0)" } ?? "") \(message)" | ||
) | ||
} | ||
|
||
public subscript( | ||
metadataKey metadataKey: String | ||
) -> Logger.Metadata.Value? { | ||
get { self.metadata[metadataKey] } | ||
set { self.metadata[metadataKey] = newValue } | ||
} | ||
} |