本节目标
- 使用 sentry 平台
- flutter 集成
- android 集成
- ios 集成
视频
代码
https://github.com/ducafecat/flutter_learn_news/releases/tag/v1.0.12
正文
错误收集策略
sentry 平台
https://sentry.io
收集 flutter
https://docs.sentry.io/platforms/flutter/
1 2
| dependencies: sentry: ^3.0.1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| final SentryClient _sentry = new SentryClient( dsn: 'https://xxxxxxxxxx', );
bool get isInDebugMode { return false; }
Future<void> _reportError(dynamic error, dynamic stackTrace) async { print('Caught error: $error'); if (isInDebugMode) { print(stackTrace); } else { final SentryResponse response = await _sentry.captureException( exception: error, stackTrace: stackTrace, );
if (response.isSuccessful) { print('Success! Event ID: ${response.eventId}'); } else { print('Failed to report to Sentry.io: ${response.error}'); } } }
Future<Null> main() async { FlutterError.onError = (FlutterErrorDetails details) async { if (isInDebugMode == true) { FlutterError.dumpErrorToConsole(details); } else { Zone.current.handleUncaughtError(details.exception, details.stack); } };
runZonedGuarded(() async { await Global.init(); runApp( MultiProvider( providers: [ ChangeNotifierProvider<AppState>.value( value: Global.appState, ), ], child: Consumer<AppState>(builder: (context, appState, _) { if (appState.isGrayFilter) { return ColorFiltered( colorFilter: ColorFilter.mode(Colors.white, BlendMode.color), child: NewsApp(), ); } else { return NewsApp(); } }), ), ); }, (Object error, StackTrace stack) { _reportError(error, stack); }); }
|
收集 android
https://docs.sentry.io/platforms/android/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| // ADD JCENTER REPOSITORY repositories { jcenter() }
// ADD COMPATIBILITY OPTIONS TO BE COMPATIBLE WITH JAVA 1.8 android { compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } }
// ADD SENTRY ANDROID AS A DEPENDENCY dependencies { // https://github.com/getsentry/sentry-android/releases implementation 'io.sentry:sentry-android:{version}' }
|
- android/app/src/main/AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10
| <application android:name="io.flutter.app.FlutterApplication" android:label="猫哥新闻" android:icon="@mipmap/launcher_icon">
...
<meta-data android:name="io.sentry.dsn" android:value="xxxxxxxxxxxxxxxxx" /> </application>
|
- android/app/src/main/kotlin/com/example/flutterducafecatnews/CrashHandler.java
1 2 3 4 5 6 7
| public class CrashHandler implements UncaughtExceptionHandler {
@Override public void uncaughtException(Thread t, Throwable e) { Sentry.captureException(e); } }
|
- android/app/src/main/kotlin/com/example/flutterducafecatnews/MainActivity.kt
1 2 3 4 5 6 7 8 9
| import io.sentry.core.Sentry
class MainActivity: FlutterActivity() { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { val crashHandler = CrashHandler() Thread.setDefaultUncaughtExceptionHandler(crashHandler) GeneratedPluginRegistrant.registerWith(flutterEngine) } }
|
收集 ios
https://docs.sentry.io/platforms/cocoa/?_ga=2.17974013.534595501.1591172359-228174411.1591172359&_gac=1.12380800.1591172359.EAIaIQobChMIrICd9Jrl6QIVCj5gCh2zFw8lEAAYASAAEgJwyfD_BwE&platform=javascript
1 2 3 4 5 6
| platform :ios, '8.0' use_frameworks! # This is important
target 'YourApp' do pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '5.1.2' end
|
- ios/Runner/AppDelegate.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| {
SentrySDK.start(options: [ "dsn": "https://xxxxxxxxxxxxxxxxxxx", "debug": true, "enableAutoSessionTracking": true ])
NSSetUncaughtExceptionHandler { exception in print(exception) SentrySDK.capture(message: exception.description) SentrySDK.capture(exception: exception) }
GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
|
资源
参考
设计稿蓝湖预览
https://lanhuapp.com/url/lYuz1
密码: gSKl
蓝湖现在收费了,所以查看标记还请自己上传 xd 设计稿
商业设计稿文件不好直接分享, 可以加微信联系 ducafecat
© 猫哥
https://ducafecat.tech
邮箱 ducafecat@gmail.com / 微信 ducafecat / 留言板 disqus