log payments using logsnag in swift

To log payments using Logsnag in Swift, you can follow the steps below:

  1. Import Logsnag in your Swift file.
main.swift
import Logsnag
15 chars
2 lines
  1. Create a Logsnag client object and set up the configuration. Be sure to replace YOUR_API_KEY with your own Logsnag API key.
main.swift
let logsnag = Logsnag(apiKey: "YOUR_API_KEY")
logsnag.configuration.releaseStage = "production"
96 chars
3 lines
  1. Log the payment event with the Logsnag client object.
main.swift
logsnag.notifyError(
    PaymentError.paymentFailed(details: "Failed to process payment"),
    groupingKey: "payment_failure"
)
128 chars
5 lines

In this example, we log an error event using the notifyError method. The PaymentError.paymentFailed is a custom error enum defined elsewhere in your project, and groupingKey is a string that you can use to group similar errors together in the Logsnag dashboard.

  1. If you want to attach additional metadata to the event, you can pass in a dictionary of key-value pairs as the second parameter of the notifyError method.
main.swift
let metadata = [
    "payment_id": "12345",
    "amount": "$19.99",
    "customer_id": "6789"
]
logsnag.notifyError(
    PaymentError.paymentFailed(details: "Failed to process payment"),
    metadata: metadata,
    groupingKey: "payment_failure"
)
248 chars
11 lines

In this example, we add three key-value pairs to the metadata dictionary: payment_id, amount, and customer_id.

That's it! With these steps, you can log payment events using Logsnag in your Swift project.

gistlibby LogSnag