diff --git a/CHANGELOG.md b/CHANGELOG.md index ddc6f8c42b..a94d8cd4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Feat: Support for Linux (#402) * Feat: Support for Windows (#407) * Fix: Mark `Sentry.currentHub` as deprecated (#406) +* Fix: Set console logger as default logger in debug mode (#413) * Fix: Use name from pubspec.yaml for release if package id is not available (#411) * Feat: `SentryHttpClient` tracks the duration which a request takes and logs failed requests (#414) * Fix: Trim `\u0000` from Windows package info (#420) diff --git a/dart/lib/src/sentry.dart b/dart/lib/src/sentry.dart index d8bd364a65..b9e61c2ec1 100644 --- a/dart/lib/src/sentry.dart +++ b/dart/lib/src/sentry.dart @@ -59,8 +59,6 @@ class Sentry { SentryOptions options, AppRunner? appRunner, ) async { - options.debug = options.platformChecker.isDebugMode(); - _setEnvironmentVariables(options); // Throws when running on the browser @@ -207,10 +205,6 @@ class Sentry { // try parsing the dsn Dsn.parse(options.dsn!); - // if logger os NoOp, let's set a logger that prints on the console - if (options.debug && options.logger == noOpLogger) { - options.logger = dartLogger; - } return true; } } diff --git a/dart/lib/src/sentry_options.dart b/dart/lib/src/sentry_options.dart index 41bbb745c4..2dd6e33b81 100644 --- a/dart/lib/src/sentry_options.dart +++ b/dart/lib/src/sentry_options.dart @@ -76,7 +76,19 @@ class SentryOptions { /// Turns debug mode on or off. If debug is enabled SDK will attempt to print out useful debugging /// information if something goes wrong. Default is disabled. - bool debug = false; + bool get debug => _debug; + + set debug(bool newValue) { + _debug = newValue; + if (_debug == true && logger == noOpLogger) { + _logger = dartLogger; + } + if (_debug == false && logger == dartLogger) { + _logger = noOpLogger; + } + } + + bool _debug = false; /// minimum LogLevel to be used if debug is enabled SentryLevel diagnosticLevel = _defaultDiagnosticLevel; @@ -160,7 +172,18 @@ class SentryOptions { /// Whether to send personal identifiable information along with events bool sendDefaultPii = false; - SentryOptions({this.dsn}) { + SentryOptions({this.dsn, PlatformChecker? checker}) { + if (checker != null) { + platformChecker = checker; + } + + // In debug mode we want to log everything by default to the console. + // In order to do that, this must be the first thing the SDK does + // and the first thing the SDK does, is to instantiate SentryOptions + if (platformChecker.isDebugMode()) { + debug = true; + } + sdk = SdkVersion(name: sdkName(platformChecker.isWeb), version: sdkVersion); sdk.addPackage('pub:sentry', sdkVersion); } diff --git a/dart/test/hub_test.dart b/dart/test/hub_test.dart index 90e1c5978a..b38ce797f5 100644 --- a/dart/test/hub_test.dart +++ b/dart/test/hub_test.dart @@ -122,12 +122,12 @@ void main() { test('should add breadcrumb to current Scope', () { hub.configureScope((Scope scope) { - expect(0, scope..breadcrumbs.length); + expect(0, scope.breadcrumbs.length); }); hub.addBreadcrumb(Breadcrumb(message: 'test')); hub.configureScope((Scope scope) { - expect(1, scope..breadcrumbs.length); - expect('test', scope..breadcrumbs.first.message); + expect(1, scope.breadcrumbs.length); + expect('test', scope.breadcrumbs.first.message); }); }); }); diff --git a/dart/test/sentry_options_test.dart b/dart/test/sentry_options_test.dart index 720c002c69..2912e009a3 100644 --- a/dart/test/sentry_options_test.dart +++ b/dart/test/sentry_options_test.dart @@ -3,6 +3,7 @@ import 'package:sentry/sentry.dart'; import 'package:sentry/src/noop_client.dart'; import 'package:test/test.dart'; +import 'fake_platform_checker.dart'; import 'mocks.dart'; void main() { @@ -32,13 +33,28 @@ void main() { expect(200, options.maxBreadcrumbs); }); - test('$SentryLogger is NoOp by default', () { - final options = SentryOptions(dsn: fakeDsn); + test('SentryLogger is NoOp by default in release mode', () { + final options = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.releaseMode()); + + expect(noOpLogger, options.logger); + }); + + test('SentryLogger is NoOp by default in profile mode', () { + final options = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.profileMode()); expect(noOpLogger, options.logger); }); - test('$SentryLogger sets a diagnostic logger', () { + test('SentryLogger is dartLogger by default in debug mode', () { + final options = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.debugMode()); + + expect(dartLogger, options.logger); + }); + + test('SentryLogger sets a diagnostic logger', () { final options = SentryOptions(dsn: fakeDsn); options.logger = dartLogger; diff --git a/dart/test/sentry_test.dart b/dart/test/sentry_test.dart index cb0cc3ac7e..7350892c96 100644 --- a/dart/test/sentry_test.dart +++ b/dart/test/sentry_test.dart @@ -190,8 +190,9 @@ void main() { }); test('options.environment profile', () async { - final sentryOptions = SentryOptions(dsn: fakeDsn) - ..platformChecker = FakePlatformChecker.profileMode(); + final sentryOptions = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.profileMode()); + await Sentry.init((options) { options.dsn = fakeDsn; expect(options.environment, 'profile'); @@ -200,12 +201,26 @@ void main() { }); test('options.environment production (defaultEnvironment)', () async { - final sentryOptions = SentryOptions(dsn: fakeDsn) - ..platformChecker = FakePlatformChecker.releaseMode(); + final sentryOptions = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.releaseMode()); + await Sentry.init((options) { options.dsn = fakeDsn; expect(options.environment, 'production'); expect(options.debug, false); }, options: sentryOptions); }); + + test('options.logger is not dartLogger after debug = false', () async { + final sentryOptions = + SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.debugMode()); + + await Sentry.init((options) { + options.dsn = fakeDsn; + expect(options.logger, dartLogger); + options.debug = false; + }, options: sentryOptions); + + expect(sentryOptions.logger == dartLogger, false); + }); }