Skip to content

Commit

Permalink
Log SDK errors to the console if the log level is fatal even if `de…
Browse files Browse the repository at this point in the history
…bug` is disabled (#1635)
  • Loading branch information
denrase authored Sep 12, 2023
1 parent 8fe0817 commit 5aa047a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Add http.request.method attribute to http spans data ([#1633](https://github.com/getsentry/sentry-dart/pull/1633))
- Add db.system and db.name attributes to db spans data ([#1629](https://github.com/getsentry/sentry-dart/pull/1629))
- Log SDK errors to the console if the log level is `fatal` even if `debug` is disabled ([#1635](https://github.com/getsentry/sentry-dart/pull/1635))

### Features

Expand Down
4 changes: 3 additions & 1 deletion dart/lib/src/diagnostic_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class DiagnosticLogger {
}

bool _isEnabled(SentryLevel level) {
return _options.debug && level.ordinal >= _options.diagnosticLevel.ordinal;
return _options.debug &&
level.ordinal >= _options.diagnosticLevel.ordinal ||
level == SentryLevel.fatal;
}
}
64 changes: 64 additions & 0 deletions dart/test/diagnostic_logger_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:sentry/sentry.dart';
import 'package:sentry/src/diagnostic_logger.dart';
import 'package:test/test.dart';

void main() {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('$DiagnosticLogger do not log if debug is disabled', () {
fixture.options.debug = false;

fixture.getSut().log(SentryLevel.error, 'foobar');

expect(fixture.loggedMessage, isNull);
});

test('$DiagnosticLogger log if debug is enabled', () {
fixture.options.debug = true;

fixture.getSut().log(SentryLevel.error, 'foobar');

expect(fixture.loggedMessage, 'foobar');
});

test('$DiagnosticLogger do not log if level is too low', () {
fixture.options.debug = true;
fixture.options.diagnosticLevel = SentryLevel.error;

fixture.getSut().log(SentryLevel.warning, 'foobar');

expect(fixture.loggedMessage, isNull);
});

test('$DiagnosticLogger always log fatal', () {
fixture.options.debug = false;

fixture.getSut().log(SentryLevel.fatal, 'foobar');

expect(fixture.loggedMessage, 'foobar');
});
}

class Fixture {
var options = SentryOptions();

Object? loggedMessage;

DiagnosticLogger getSut() {
return DiagnosticLogger(mockLogger, options);
}

void mockLogger(
SentryLevel level,
String message, {
String? logger,
Object? exception,
StackTrace? stackTrace,
}) {
loggedMessage = message;
}
}

0 comments on commit 5aa047a

Please sign in to comment.