-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
02f27b6
commit a547dbb
Showing
5 changed files
with
225 additions
and
4 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
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,105 @@ | ||
// | ||
// PrintLogHandler.swift | ||
// swift-webpush | ||
// | ||
// Created by Dimitri Bouniol on 2024-12-06. | ||
// Copyright © 2024 Mochi Development, Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Logging | ||
|
||
struct PrintLogHandler: LogHandler { | ||
private let label: String | ||
|
||
var logLevel: Logger.Level = .info | ||
var metadataProvider: Logger.MetadataProvider? | ||
|
||
init( | ||
label: String, | ||
logLevel: Logger.Level = .info, | ||
metadataProvider: Logger.MetadataProvider? = nil | ||
) { | ||
self.label = label | ||
self.logLevel = logLevel | ||
self.metadataProvider = metadataProvider | ||
} | ||
|
||
private var prettyMetadata: String? | ||
var metadata = Logger.Metadata() { | ||
didSet { | ||
self.prettyMetadata = self.prettify(self.metadata) | ||
} | ||
} | ||
|
||
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { | ||
get { | ||
self.metadata[metadataKey] | ||
} | ||
set { | ||
self.metadata[metadataKey] = newValue | ||
} | ||
} | ||
|
||
func log( | ||
level: Logger.Level, | ||
message: Logger.Message, | ||
metadata explicitMetadata: Logger.Metadata?, | ||
source: String, | ||
file: String, | ||
function: String, | ||
line: UInt | ||
) { | ||
let effectiveMetadata = Self.prepareMetadata( | ||
base: self.metadata, | ||
provider: self.metadataProvider, | ||
explicit: explicitMetadata | ||
) | ||
|
||
let prettyMetadata: String? | ||
if let effectiveMetadata = effectiveMetadata { | ||
prettyMetadata = self.prettify(effectiveMetadata) | ||
} else { | ||
prettyMetadata = self.prettyMetadata | ||
} | ||
|
||
print("\(self.timestamp()) [\(level)] \(self.label):\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)") | ||
} | ||
|
||
internal static func prepareMetadata( | ||
base: Logger.Metadata, | ||
provider: Logger.MetadataProvider?, | ||
explicit: Logger.Metadata? | ||
) -> Logger.Metadata? { | ||
var metadata = base | ||
|
||
let provided = provider?.get() ?? [:] | ||
|
||
guard !provided.isEmpty || !((explicit ?? [:]).isEmpty) else { | ||
// all per-log-statement values are empty | ||
return nil | ||
} | ||
|
||
if !provided.isEmpty { | ||
metadata.merge(provided, uniquingKeysWith: { _, provided in provided }) | ||
} | ||
|
||
if let explicit = explicit, !explicit.isEmpty { | ||
metadata.merge(explicit, uniquingKeysWith: { _, explicit in explicit }) | ||
} | ||
|
||
return metadata | ||
} | ||
|
||
private func prettify(_ metadata: Logger.Metadata) -> String? { | ||
if metadata.isEmpty { | ||
return nil | ||
} else { | ||
return metadata.lazy.sorted(by: { $0.key < $1.key }).map { "\($0)=\($1)" }.joined(separator: " ") | ||
} | ||
} | ||
|
||
private func timestamp() -> String { | ||
Date().formatted(date: .numeric, time: .complete) | ||
} | ||
} |
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
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,19 @@ | ||
// | ||
// VAPIDConfiguration+Testing.swift | ||
// swift-webpush | ||
// | ||
// Created by Dimitri Bouniol on 2024-12-06. | ||
// Copyright © 2024 Mochi Development, Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import WebPush | ||
|
||
extension VAPID.Configuration { | ||
static func makeTesting() -> VAPID.Configuration { | ||
VAPID.Configuration( | ||
key: VAPID.Key(), | ||
contactInformation: .url(URL(string: "https://example.com/contact")!) | ||
) | ||
} | ||
} |
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