Skip to content

Fix: Use dartLogger as default in debug mode #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions dart/lib/src/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class Sentry {
SentryOptions options,
AppRunner? appRunner,
) async {
options.debug = options.platformChecker.isDebugMode();

_setEnvironmentVariables(options);

// Throws when running on the browser
Expand Down Expand Up @@ -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;
}
}
27 changes: 25 additions & 2 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions dart/test/hub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down
22 changes: 19 additions & 3 deletions dart/test/sentry_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;

Expand Down
23 changes: 19 additions & 4 deletions dart/test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
});
}