diff --git a/CHANGELOG.md b/CHANGELOG.md index 415a645cd6..51812e27c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +* Feat: Add logger name to `SentryLogger` and send errors in integrations to the registered logger (#641) + # 6.1.2 * Fix: Remove is Enum check to support older Dart versions (#635) diff --git a/dart/lib/src/default_integrations.dart b/dart/lib/src/default_integrations.dart index 3138d2703d..460aeeef41 100644 --- a/dart/lib/src/default_integrations.dart +++ b/dart/lib/src/default_integrations.dart @@ -27,6 +27,14 @@ class RunZonedGuardedIntegration extends Integration { await _runner(); }, (exception, stackTrace) async { + options.logger( + SentryLevel.error, + 'Uncaught zone error', + logger: 'sentry.runZonedGuarded', + exception: exception, + stackTrace: stackTrace, + ); + // runZonedGuarded doesn't crash the App. final mechanism = Mechanism(type: 'runZonedGuarded', handled: true); final throwableMechanism = ThrowableMechanism(mechanism, exception); diff --git a/dart/lib/src/diagnostic_logger.dart b/dart/lib/src/diagnostic_logger.dart index 82b6705ede..9a393f974f 100644 --- a/dart/lib/src/diagnostic_logger.dart +++ b/dart/lib/src/diagnostic_logger.dart @@ -10,11 +10,18 @@ class DiagnosticLogger { void log( SentryLevel level, String message, { + String? logger, Object? exception, StackTrace? stackTrace, }) { if (_isEnabled(level)) { - _logger(level, message, exception: exception, stackTrace: stackTrace); + _logger( + level, + message, + logger: logger, + exception: exception, + stackTrace: stackTrace, + ); } } diff --git a/dart/lib/src/isolate_error_integration.dart b/dart/lib/src/isolate_error_integration.dart index 95a39e0442..600026e4ff 100644 --- a/dart/lib/src/isolate_error_integration.dart +++ b/dart/lib/src/isolate_error_integration.dart @@ -48,9 +48,23 @@ Future handleIsolateError( // https://api.dartlang.org/stable/2.7.0/dart-isolate/Isolate/addErrorListener.html // error is a list of 2 elements - if (error is List && error.length == 2) { - final dynamic throwable = error.first; - final dynamic stackTrace = error.last; + if (error is List && error.length == 2) { + /// The errors are sent back as two-element lists. + /// The first element is a `String` representation of the error, usually + /// created by calling `toString` on the error. + /// The second element is a `String` representation of an accompanying + /// stack trace, or `null` if no stack trace was provided. + /// To convert this back to a [StackTrace] object, use [StackTrace.fromString]. + final String throwable = error.first; + final String? stackTrace = error.last; + + options.logger( + SentryLevel.error, + 'Uncaught isolate error', + logger: 'sentry.isolateError', + exception: throwable, + stackTrace: stackTrace == null ? null : StackTrace.fromString(stackTrace), + ); // Isolate errors don't crash the App. final mechanism = Mechanism(type: 'isolateError', handled: true); diff --git a/dart/lib/src/sentry_options.dart b/dart/lib/src/sentry_options.dart index 7ef88bf336..64adfaeaa4 100644 --- a/dart/lib/src/sentry_options.dart +++ b/dart/lib/src/sentry_options.dart @@ -6,16 +6,16 @@ import 'package:http/http.dart'; import 'diagnostic_logger.dart'; import 'environment/environment_variables.dart'; import 'event_processor.dart'; +import 'http_client/sentry_http_client.dart'; import 'integration.dart'; import 'noop_client.dart'; +import 'platform_checker.dart'; import 'protocol.dart'; import 'tracing.dart'; import 'transport/noop_transport.dart'; import 'transport/transport.dart'; import 'utils.dart'; import 'version.dart'; -import 'platform_checker.dart'; -import 'http_client/sentry_http_client.dart'; // TODO: Scope observers, enableScopeSync // TODO: shutdownTimeout, flushTimeoutMillis @@ -320,6 +320,7 @@ typedef ClockProvider = DateTime Function(); typedef SentryLogger = void Function( SentryLevel level, String message, { + String? logger, Object? exception, StackTrace? stackTrace, }); @@ -331,6 +332,7 @@ typedef TracesSamplerCallback = double? Function( void noOpLogger( SentryLevel level, String message, { + String? logger, Object? exception, StackTrace? stackTrace, }) {} @@ -339,13 +341,14 @@ void noOpLogger( void dartLogger( SentryLevel level, String message, { + String? logger, Object? exception, StackTrace? stackTrace, }) { log( '[${level.name}] $message', level: level.toDartLogLevel(), - name: 'sentry', + name: logger ?? 'sentry', time: getUtcDateTime(), error: exception, stackTrace: stackTrace, diff --git a/dart/test/default_integrations_test.dart b/dart/test/default_integrations_test.dart index 3c9e865503..6ca1bb64ec 100644 --- a/dart/test/default_integrations_test.dart +++ b/dart/test/default_integrations_test.dart @@ -2,8 +2,8 @@ import 'package:sentry/sentry.dart'; import 'package:sentry/src/noop_sentry_span.dart'; import 'package:test/test.dart'; -import 'mocks.dart'; import 'mocks/mock_hub.dart'; +import 'mocks.dart'; void main() { late Fixture fixture; @@ -34,8 +34,8 @@ void main() { test( 'Isolate error capture errors', () async { - final throwable = StateError('error'); - final stackTrace = StackTrace.current; + final throwable = StateError('error').toString(); + final stackTrace = StackTrace.current.toString(); final error = [throwable, stackTrace]; // we could not find a way to trigger an error to the current Isolate diff --git a/flutter/example/pubspec.yaml b/flutter/example/pubspec.yaml index bb1bd27afc..2fd1576045 100644 --- a/flutter/example/pubspec.yaml +++ b/flutter/example/pubspec.yaml @@ -11,8 +11,8 @@ environment: dependencies: flutter: sdk: flutter + sentry: sentry_flutter: - path: ../ universal_platform: ^1.0.0-nullsafety feedback: ^2.0.0 provider: ^6.0.0 @@ -20,6 +20,12 @@ dependencies: dev_dependencies: sentry_dart_plugin: ^1.0.0-alpha.4 +dependency_overrides: + sentry: + path: ../../dart + sentry_flutter: + path: ../ + flutter: uses-material-design: true diff --git a/flutter/lib/src/default_integrations.dart b/flutter/lib/src/default_integrations.dart index 13e6c3c1af..2404bd2501 100644 --- a/flutter/lib/src/default_integrations.dart +++ b/flutter/lib/src/default_integrations.dart @@ -1,10 +1,11 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; -import 'package:flutter/widgets.dart'; import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:sentry/sentry.dart'; + import 'sentry_flutter_options.dart'; import 'widgets_binding_observer.dart'; @@ -70,6 +71,14 @@ class FlutterErrorIntegration extends Integration { if (library != null) 'library': library, }; + options.logger( + SentryLevel.error, + errorDetails.toStringShort(), + logger: 'sentry.flutterError', + exception: exception, + stackTrace: errorDetails.stack, + ); + // FlutterError doesn't crash the App. final mechanism = Mechanism( type: 'FlutterError',