如何在Crashlytics Swift中打印自定义日志记录? [英] How to print customize logging in Crashlytics Swift?

查看:157
本文介绍了如何在Crashlytics Swift中打印自定义日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经为Fabric和crashlytics安装了pod文件,并将其导入到应用程序委托中。 didFinishLaunchingWithOptions 函数中的 Fabric.with([Crashlytics.self,Branch.self])。一切正常。但是我想打印用户可以单击哪个动作。 例如)点击登录按钮。

We have install pod file for fabric and crashlytics and imported in the app delegate. Fabric.with([Crashlytics.self, Branch.self]) in didFinishLaunchingWithOptions function. It's working fine. But I want to print which action could be clicked by the user. Eg) Clicked Login Button.

我看到我们可以调用此函数,但是在哪里可以调用此函数?

I saw we can call this function, But where can i call this?

func write(string: String) {
        CLSLogv("%@", getVaList([string]))
    }

我们将无法从这次崩溃中找到代码行

we won't be able to find the line of code from this crash

如何在结构中打印自定义日志?
崩溃时会调用哪个默认函数?

How to print custom logs in fabric? Which default function get called while getting crash?

推荐答案

您可以使用logEvent函数创建 CrashLogger 类,该函数将eventName作为 String 和数据字典,其格式为 [String:CustomStringConvertible] 格式,以将任何相关信息传递给Crashlytics

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]))
    }
}

现在,只要您想在Crashlytics中记录自定义事件,就可以调用此 logEvent 方法,它将在日志部分中可用当您在Firebase中查看任何崩溃时。

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.

使用方法:,例如 addToCart 电子商务应用程序中的功能:

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.
}

请参阅Crashlytics自定义日志记录文档了解更多信息。

Refer Crashlytics custom logging docs for further info.

这篇关于如何在Crashlytics Swift中打印自定义日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆