近段时间在研究崩溃日志的获取方式,经过搜寻找到开源库PLCrashReporter和KSCrash。第三方则有Sentry、友盟、Bugly等。目前我们项目中使用了Sentry和PLCrashReporter。
PLCrashReporter
PLCrashReporter的集成
pod 'PLCrashReporter', '~> 1.8.1'
PLCrashReporter使用
#import <CrashReporter/CrashReporter.h>
PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach
symbolicationStrategy: PLCrashReporterSymbolicationStrategyNone];
PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration: config];
// Enable the Crash Reporter.
NSError *error;
if (![crashReporter enableCrashReporterAndReturnError: &error]) {
NSLog(@"Warning: Could not enable crash reporter: %@", error);
}
if ([crashReporter hasPendingCrashReport]) {
NSError *error;
// Try loading the crash report.
NSData *data = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
if (data == nil) {
NSLog(@"Failed to load crash report data: %@", error);
return;
}
// Retrieving crash reporter data.
PLCrashReport *report = [[PLCrashReport alloc] initWithData: data error: &error];
if (report == nil) {
NSLog(@"Failed to parse crash report: %@", error);
return;
}
// We could send the report from here, but we'll just print out some debugging info instead.
// 这是获取的日志信息,可以自己保存到沙盒中,然后上传自己的服务器。
NSString *text = [PLCrashReportTextFormatter stringValueForCrashReport: report withTextFormat: PLCrashReportTextFormatiOS];
NSLog(@"%@", text);
// Purge the report.
[crashReporter purgePendingCrashReport];
}
Sentry
Sentry的使用方式在之前的文章都已经提过。
iOS端实现sentry日志收集
Sentry和PLCrashReporter之间的冲突
如果先把PLCrashReporter的使用写在sentry代码之后,就会出现sentry不会自动上传崩溃日志。
正确做法是:
其他崩溃日志的获取在sentry之前写。
例如:
//pLCrashReporter的使用。
[self pLCrashReporter];
[SentrySDK startWithConfigureOptions:^(SentryOptions * _Nonnull options) {
options.dsn = @"http://304ca6bc430645aea25082b1c6426a79@192.168.43.152:9000/2";
options.debug = YES;
}];
-(void)pLCrashReporter{
PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach
symbolicationStrategy: PLCrashReporterSymbolicationStrategyNone];
PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration: config];
// Enable the Crash Reporter.
NSError *error;
if (![crashReporter enableCrashReporterAndReturnError: &error]) {
NSLog(@"Warning: Could not enable crash reporter: %@", error);
}
if ([crashReporter hasPendingCrashReport]) {
NSError *error;
// Try loading the crash report.
NSData *data = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
if (data == nil) {
NSLog(@"Failed to load crash report data: %@", error);
return;
}
// Retrieving crash reporter data.
PLCrashReport *report = [[PLCrashReport alloc] initWithData: data error: &error];
if (report == nil) {
NSLog(@"Failed to parse crash report: %@", error);
return;
}
// We could send the report from here, but we'll just print out some debugging info instead.
// 这是获取的日志信息,可以自己保存到沙盒中,然后上传自己的服务器。
NSString *text = [PLCrashReportTextFormatter stringValueForCrashReport: report withTextFormat: PLCrashReportTextFormatiOS];
NSLog(@"%@", text);
// Purge the report.
[crashReporter purgePendingCrashReport];
}
}
问题解决。如有问题,请联系。