Refactor class to use protocol

This commit is contained in:
Angelo Stavrow 2022-06-25 12:07:38 -04:00
parent 9b2572ba41
commit b93e0c3547
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -7,16 +7,26 @@
import Foundation import Foundation
import os import os
import OSLog
protocol LogWriter {
func log(_ message: String, withSensitiveInfo privateInfo: String?, level: OSLogType)
func logCrashAndSetFlag(error: Error)
}
final class Logging { final class Logging {
private let logger: Logger private let logger: Logger
private let subsystem = Bundle.main.bundleIdentifier! private let subsystem = Bundle.main.bundleIdentifier!
init(for category: String) { init(for category: String = "") {
self.logger = Logger(subsystem: subsystem, category: category) self.logger = Logger(subsystem: subsystem, category: category)
} }
}
extension Logging: LogWriter {
func log( func log(
_ message: String, _ message: String,
withSensitiveInfo privateInfo: String? = nil, withSensitiveInfo privateInfo: String? = nil,
@ -33,7 +43,7 @@ final class Logging {
let errorDescription = error.localizedDescription let errorDescription = error.localizedDescription
UserDefaults.shared.set(true, forKey: WFDefaults.didHaveFatalError) UserDefaults.shared.set(true, forKey: WFDefaults.didHaveFatalError)
UserDefaults.shared.set(errorDescription, forKey: WFDefaults.fatalErrorDescription) UserDefaults.shared.set(errorDescription, forKey: WFDefaults.fatalErrorDescription)
logger.critical("\(errorDescription)") logger.log(level: .error, "\(errorDescription)")
fatalError(errorDescription) fatalError(errorDescription)
} }