From 85747b146ea638b31de1ceb3a287679da539ad7d Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Tue, 30 Apr 2024 20:35:30 -0400 Subject: [PATCH] Digital PCT265 story ONEAPP-7249 - Pipe logs to system logger rather than stdout. Logging updates to categorize logs. --- .../MVMCoreLoggingDelegateProtocol.swift | 9 ++++-- .../MVMCoreLoggingHandler+Extension.swift | 17 +++++------ .../OtherHandlers/MVMCoreLoggingHandler.swift | 30 +++++++++++++++++-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/MVMCore/MVMCore/MainProtocols/MVMCoreLoggingDelegateProtocol.swift b/MVMCore/MVMCore/MainProtocols/MVMCoreLoggingDelegateProtocol.swift index eb6f223..b903a09 100644 --- a/MVMCore/MVMCore/MainProtocols/MVMCoreLoggingDelegateProtocol.swift +++ b/MVMCore/MVMCore/MainProtocols/MVMCoreLoggingDelegateProtocol.swift @@ -8,13 +8,16 @@ public protocol MVMCoreLoggingDelegateProtocol { - // Can be used to log different actions performed by the core. + /// Can be used to log different actions performed by the core. func handleDebugMessage(_ message: String?) + + /// Can be used to log a message under a particular cagetory. + func handleDebugMessage(_ message: String, category: String?) - // Can be used to choose how to log error objects. + /// Can be used to choose how to log error objects. func addError(toLog errorObject: MVMCoreErrorObject) - // Log that the load has finished. + /// Log that the load has finished. func logLoadFinished(_ loadObject: MVMCoreLoadObject?, loadedViewController: MVMCoreViewControllerProtocol?, error: MVMCoreErrorObject?) } diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler+Extension.swift b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler+Extension.swift index cc80a81..e322f37 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler+Extension.swift +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler+Extension.swift @@ -7,34 +7,31 @@ // import Foundation - -@objc public extension MVMCoreLoggingHandler { - @objc func print(with message: String) { - Swift.print(message) - } -} +import os public protocol CoreLogging { - func debugLog(_ string: String) + static var loggingCategory: String? { get } var loggingPrefix: String { get } } public extension CoreLogging { + static var loggingCategory: String? { return nil } + var loggingPrefix: String { - return String(describing: self) + return "" } static func debugLog(_ string: String) { #if LOGGING - MVMCoreLoggingHandler.shared()?.handleDebugMessage("\(String(describing: Self.self)) \(string)") + MVMCoreLoggingHandler.shared()?.handleDebugMessage(string, category: loggingCategory) #endif } func debugLog(_ string: String) { #if LOGGING - MVMCoreLoggingHandler.shared()?.handleDebugMessage("\(loggingPrefix) " + string) + MVMCoreLoggingHandler.shared()?.handleDebugMessage("\(loggingPrefix)\(string)", category: Self.loggingCategory) #endif } } diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.swift b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.swift index 1732fd5..cb0ee9c 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.swift +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.swift @@ -7,9 +7,28 @@ // import Foundation +import os @objc open class MVMCoreLoggingHandler: NSObject, MVMCoreLoggingDelegateProtocol { + public static let standardCategory = "General" + + private let logger = Logger(subsystem: "MVMCoreLogging", category: standardCategory) + private var loggerCache = [String: Logger]() + + open func getLogger(category: String?) -> Logger { + if let category { + if let logger = loggerCache[category] { + return logger + } else { + let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: category) + loggerCache[category] = logger + return logger + } + } + return logger + } + @objc(sharedLoggingHandler) public static func shared() -> Self? { return MVMCoreActionUtility.initializerClassCheck(MVMCoreObject.sharedInstance()?.loggingDelegate as? NSObject, classToVerify: self) as? Self @@ -29,8 +48,15 @@ import Foundation // MARK: - logging delegate @objc open func handleDebugMessage(_ message: String?) { #if LOGGING - guard let message = message else { return } - self.print(with: message) + guard let message = message else { return } + logger.debug("\(message, privacy: .public)") // Assume that becaues this is a LOGGING build we want these messages to be unmasked. + // TODO: How do we split the messaging by Library and Subsystem? + #endif + } + + open func handleDebugMessage(_ message: String, category: String?) { + #if LOGGING + getLogger(category: category).debug("\(message, privacy: .public)") // Assume that becaues this is a LOGGING build we want these messages to be unmasked. #endif }