You can create CrashLogger class with logEvent function which takes eventName as String and data dictionary which is [String: CustomStringConvertible] format to pass any relevant info to Crashlytics
import Crashlytics
final class CrashLogger {
static let shared = CrashLogger()
private init() { }
func logEvent(_ event: String, withData data: [String: CustomStringConvertible]) {
let dataString = data.reduce("Event: \(event): ", { (result, element: (key: String, value: CustomStringConvertible)) -> String in
return result + " (" + element.key + ": " + String(describing: element.value) + " )"
})
logEvent(dataString)
}
private func logEvent(_ message: String) {
CLSLogv("%@", getVaList([message]))
}
}
Now you can call this logEvent method whenever you want to log the custom event in Crashlytics and it will be available in logs section when you view any crash in Firebase.
How to use: for example addToCart function in eCommerce application:
func addToCard(_ product: Product) {
CrashLogger.logEvent("addToCart", withData: ["productId": product.id, "productName": product.name)
//do further processing like update cart item count etc.
}
Refer Crashlytics custom logging docs for further info.