-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add debuglogger to confidence (#144)
* add debuglogger to confidence * fix lint issues * add logging to RemoteConfidenceClient * add test coverage * align with android * liter fix * adjust data logged * document how logging works --------- Co-authored-by: Nicklas Lundin <[email protected]>
- Loading branch information
1 parent
2ffc41d
commit c8fe939
Showing
9 changed files
with
199 additions
and
35 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
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
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,69 @@ | ||
import Foundation | ||
import OSLog | ||
|
||
internal protocol DebugLogger { | ||
func logEvent(action: String, event: ConfidenceEvent?) | ||
func logMessage(message: String, isWarning: Bool) | ||
func logFlags(action: String, flag: String) | ||
func logContext(action: String, context: ConfidenceStruct) | ||
} | ||
|
||
private extension Logger { | ||
private static var subsystem = Bundle.main.bundleIdentifier | ||
|
||
static let confidence = Logger(subsystem: subsystem ?? "", category: "confidence") | ||
} | ||
|
||
internal class DebugLoggerImpl: DebugLogger { | ||
private let loggerLevel: LoggerLevel | ||
|
||
init(loggerLevel: LoggerLevel) { | ||
self.loggerLevel = loggerLevel | ||
} | ||
|
||
func logMessage(message: String, isWarning: Bool = false) { | ||
if isWarning { | ||
log(messageLevel: .WARN, message: message) | ||
} else { | ||
log(messageLevel: .DEBUG, message: message) | ||
} | ||
} | ||
|
||
func logEvent(action: String, event: ConfidenceEvent?) { | ||
log(messageLevel: .DEBUG, message: "[\(action)] \(event?.name ?? "")") | ||
} | ||
|
||
func logFlags(action: String, flag: String) { | ||
log(messageLevel: .TRACE, message: "[\(action)] \(flag)") | ||
} | ||
|
||
func logContext(action: String, context: ConfidenceStruct) { | ||
log(messageLevel: .TRACE, message: "[\(action)] \(context)") | ||
} | ||
|
||
private func log(messageLevel: LoggerLevel, message: String) { | ||
if messageLevel >= loggerLevel { | ||
switch messageLevel { | ||
case .TRACE: | ||
Logger.confidence.trace("\(message)") | ||
case .DEBUG: | ||
Logger.confidence.debug("\(message)") | ||
case .WARN: | ||
Logger.confidence.warning("\(message)") | ||
case .ERROR: | ||
Logger.confidence.error("\(message)") | ||
case .NONE: | ||
// do nothing | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
public enum LoggerLevel: Comparable { | ||
case TRACE | ||
case DEBUG | ||
case WARN | ||
case ERROR | ||
case NONE | ||
} |
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
Oops, something went wrong.