-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2378 from woocommerce/release/4.4
Merge Release/4.4 back into develop
- Loading branch information
Showing
16 changed files
with
311 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// The level of severity, that is currently based on `SentrySeverity`. | ||
public enum SeverityLevel { | ||
case fatal | ||
case error | ||
case warning | ||
case info | ||
case debug | ||
} | ||
|
||
/// Logs crashes or messages at a given severity level. | ||
public protocol CrashLogger { | ||
/** | ||
Writes a message to the Crash Logging system. | ||
- Parameters: | ||
- message: The message | ||
- properties: A dictionary containing additional information about this message | ||
- level: The level of severity to report | ||
*/ | ||
func logMessage(_ message: String, properties: [String: Any]?, level: SeverityLevel) | ||
|
||
/** | ||
Writes a message to the Crash Logging system and waits until the message is sent. | ||
- Parameters: | ||
- message: The message | ||
- properties: A dictionary containing additional information about this message | ||
- level: The level of severity to report | ||
*/ | ||
func logMessageAndWait(_ message: String, properties: [String: Any]?, level: SeverityLevel) | ||
} |
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,11 @@ | ||
import Storage | ||
|
||
struct MockCrashLogger: CrashLogger { | ||
func logMessage(_ message: String, properties: [String: Any]?, level: SeverityLevel) { | ||
// no-op | ||
} | ||
|
||
func logMessageAndWait(_ message: String, properties: [String: Any]?, level: SeverityLevel) { | ||
// no-op | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
WooCommerce/Classes/Tools/Logging/Dictionary+Logging.swift
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,32 @@ | ||
import Foundation | ||
|
||
extension Dictionary where Key == String { | ||
/// Manually serializes a value in a dictionary if the value is not already serializable. | ||
func serializeValuesForLoggingIfNeeded() -> [String: Any] { | ||
guard JSONSerialization.isValidJSONObject(self) == false else { | ||
return self | ||
} | ||
|
||
return reduce([:]) { (properties, entry) -> [String: Any] in | ||
let (key, value) = entry | ||
var formattedProperties: [String: Any] = properties | ||
guard JSONSerialization.isValidJSONObject([key: value]) == false else { | ||
formattedProperties[key] = value | ||
return formattedProperties | ||
} | ||
|
||
if let nsError = value as? NSError { | ||
formattedProperties[key] = [ | ||
"Domain": nsError.domain, | ||
"Code": nsError.code, | ||
"Description": nsError.localizedDescription, | ||
"User Info": nsError.userInfo.description | ||
] | ||
return formattedProperties | ||
} | ||
|
||
formattedProperties[key] = "\(value)" | ||
return formattedProperties | ||
} | ||
} | ||
} |
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,62 @@ | ||
import AutomatticTracks | ||
import Sentry | ||
import Storage | ||
|
||
/// Logs crashes/messages to Sentry. | ||
final class SentryCrashLogger: CrashLogger { | ||
func logMessage(_ message: String, properties: [String: Any]?, level: SeverityLevel) { | ||
CrashLogging.logMessage(message, properties: properties?.serializeValuesForLoggingIfNeeded(), level: SentrySeverity(level: level)) | ||
} | ||
|
||
func logMessageAndWait(_ message: String, properties: [String: Any]?, level: SeverityLevel) { | ||
CrashLogging.logMessageAndWait(message, properties: properties?.serializeValuesForLoggingIfNeeded(), level: SentrySeverity(level: level)) | ||
} | ||
} | ||
|
||
private extension CrashLogging { | ||
/** | ||
Mostly similar to `logMessage(_:properties:level:)`, but this function blocks the thread until the event is fired. | ||
- Parameters: | ||
- message: The message | ||
- properties: A dictionary containing additional information about this error | ||
- level: The level of severity to report in Sentry | ||
*/ | ||
static func logMessageAndWait(_ message: String, properties: [String: Any]?, level: SentrySeverity) { | ||
let event = Event(level: level) | ||
event.message = message | ||
event.extra = properties | ||
|
||
Client.shared?.snapshotStacktrace { | ||
Client.shared?.appendStacktrace(to: event) | ||
} | ||
|
||
guard let client = Client.shared else { | ||
return | ||
} | ||
|
||
let semaphore = DispatchSemaphore(value: 0) | ||
|
||
client.send(event: event) { _ in | ||
semaphore.signal() | ||
} | ||
|
||
semaphore.wait() | ||
} | ||
} | ||
|
||
private extension SentrySeverity { | ||
init(level: SeverityLevel) { | ||
switch level { | ||
case .fatal: | ||
self = .fatal | ||
case .error: | ||
self = .error | ||
case .warning: | ||
self = .warning | ||
case .info: | ||
self = .info | ||
case .debug: | ||
self = .debug | ||
} | ||
} | ||
} |
Oops, something went wrong.