一个使用 Swift 编写的 Sentry 错误报告 API 的客户端。
let client = Sentry.shared
do {
// create a DSN object from your DSN string (found in Sentry panel).
let dsn = try DSN(dsn: "https://public:[email protected]/1")
// configure the shared client with the DSN
client.dsn = dsn
} catch {
// handle error
}
// other attributes you may set at start or when they become available
client.hostVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
client.environment = "production"
// the context allows you to store general information to include with requests
// i.e. tags, extra metadata, and user information (more info in Xcode)
client.context.tags = ["customer_tier": "pro"]
client.context.extra = ["advertising_group": "alucard"]
// you should set this attribute of context when a user authenticates
client.context.user = User(id:username:email:extra)
您可以使用客户端中的 captureMessage(message:level:tags:extra:)
方法捕获消息。
do {
try client.captureMessage("This is a test message", level: .info)
} catch {
// handle error gracefully
}
您可以使用客户端中的 captureError(message:culprit:exception:tags:extra)
方法捕获错误。
// the `Exception` struct is used to represent an error (i.e.)
// init signature: Exception(value:type:module)
let exception = Exception(value: "did fail to test", type: "NSTestDomain")
do {
try client.captureError(message: "This is a test error", culprit: "didTest() in Test.swift:12", exception: exception)
} catch {
// handle error gracefully
}
Sentry 支持一个名为面包屑的概念,它是指在问题发生之前发生的事件序列。
// create a generic breadcrumb
let readBC = Breadcrumb(category: "user.action", level: .info, message: "User read README")
client.addBreadcrumb(readBC)
// sentry supports two types of specialized breadcrumbs a `navigation` and `http`
// there's two build methods that will allow you to create those correctly
// this describes a URL change in a web app, a UI transition in a mobile application, etc.
let navBC = Breadcrumb.makeNavigationBreadcrumb(from: "launch", to: "firstViewController")
client.addBreadcrumb(navBC)
// this represents an HTTP request transmitted from your application.
let requestBC = Breadcrumb.makeHttpBreadcrumb(url: "http://dcvz.io", method: "GET", statusCode: 200, reason: "OK")
client.addBreadcrumb(requestBC)
// you may also clear breadcrumbs
client.clearBreadcrumbs()
您也可以在 Twitter 上找到我:@dchavezlive
SentryKit 在 MIT 许可证下发布。有关详情,请参阅 LICENSE。