-
-
Notifications
You must be signed in to change notification settings - Fork 277
Add integration for logging package #631
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
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e35f77f
Logging Integration
ueman a3852fb
CI
ueman 5e6f17a
Readme
ueman 4e8e47c
fix path
ueman dc02e35
only build logging when necessary
ueman 5ddae9d
Update sentry_logging/pubspec.yaml
ueman 74b7cce
Update sentry_logging/pubspec.yaml
ueman 020462a
always build
ueman 326fe43
pr feedback
ueman 4ebcde8
don't build dart/flutter if just logging is changed
ueman 96894a7
fix ci
ueman 820ec00
fix coverage
ueman 8d83f84
Update sentry_logging/CHANGELOG.md
ueman 66c974a
Update sentry_logging/lib/src/version.dart
ueman a229695
use async await
ueman 72b5d2c
remove to string
ueman dd2f5fa
Rename folder to logging + symlink changelog
ueman c905ab2
Merge remote-tracking branch 'origin/main' into feat/logging
ueman 04c4fcc
PR Feedback
ueman 0d4805b
return some value
ueman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Logging Integration
- Loading branch information
commit e35f77f1f4c8e4b2c48556650a6c3c5cad260fef
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Files and directories created by pub. | ||
| .dart_tool/ | ||
| .packages | ||
|
|
||
| # Conventional directory for build outputs. | ||
| build/ | ||
|
|
||
| # Omit committing pubspec.lock for library packages; see | ||
| # https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
| pubspec.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 1.0.0 | ||
|
|
||
| - Initial version. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # WIP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| include: package:lints/recommended.yaml | ||
|
|
||
| analyzer: | ||
| strong-mode: | ||
| implicit-casts: false | ||
| implicit-dynamic: false | ||
| language: | ||
| strict-raw-types: true | ||
| errors: | ||
| # treat missing required parameters as a warning (not a hint) | ||
| missing_required_param: error | ||
| # treat missing returns as a warning (not a hint) | ||
| missing_return: error | ||
| # allow having TODOs in the code | ||
| todo: ignore | ||
| # allow self-reference to deprecated members (we do this because otherwise we have | ||
| # to annotate every member in every test, assert, etc, when we deprecate something) | ||
| deprecated_member_use_from_same_package: warning | ||
| # ignore sentry/path on pubspec as we change it on deployment | ||
| invalid_dependency: ignore | ||
| exclude: | ||
| - example/** | ||
|
|
||
| linter: | ||
| rules: | ||
| - prefer_final_locals | ||
| - public_member_api_docs | ||
| - prefer_single_quotes | ||
| - prefer_relative_imports | ||
| - unnecessary_brace_in_string_interps | ||
| - implementation_imports | ||
| - require_trailing_commas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import 'package:sentry_logging/sentry_logging.dart'; | ||
| import 'dart:async'; | ||
| import 'package:sentry/sentry.dart'; | ||
| import 'package:logging/logging.dart'; | ||
|
|
||
| Future<void> main() async { | ||
| // ATTENTION: Change the DSN below with your own to see the events in Sentry. Get one at sentry.io | ||
| const dsn = | ||
| 'https://[email protected]/5428562'; | ||
|
|
||
| await Sentry.init( | ||
| (options) { | ||
| options.dsn = dsn; | ||
| options.addIntegration(LoggingIntegration()); | ||
| }, | ||
| appRunner: runApp, | ||
| ); | ||
| } | ||
|
|
||
| Future<void> runApp() async { | ||
| final log = Logger('MyAwesomeLogger'); | ||
|
|
||
| log.warning('a warning!'); | ||
|
|
||
| try { | ||
| throw Exception(); | ||
| } catch (error, stackTrace) { | ||
| // The log from above will be contained in this crash report. | ||
| await Sentry.captureException( | ||
| error, | ||
| stackTrace: stackTrace, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| library sentry_logging; | ||
|
|
||
| export 'src/logging_integration.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // ignore_for_file: public_member_api_docs | ||
|
|
||
| import 'package:logging/logging.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
| import 'package:sentry/sentry_io.dart'; | ||
|
|
||
| extension LogRecordX on LogRecord { | ||
| Breadcrumb toBreadcrumb() { | ||
| return Breadcrumb( | ||
| category: 'log', | ||
| type: 'debug', | ||
| timestamp: time, | ||
| level: level.toSentryLevel(), | ||
| message: message, | ||
| data: <String, Object>{ | ||
| if (object != null) 'LogRecord.object': object!.toString(), | ||
| if (error != null) 'LogRecord.error': error!.toString(), | ||
| if (stackTrace != null) 'LogRecord.stackTrace': stackTrace!.toString(), | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'LogRecord.loggerName': loggerName, | ||
| 'LogRecord.sequenceNumber': sequenceNumber, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| SentryEvent toEvent() { | ||
| return SentryEvent( | ||
| logger: loggerName, | ||
| level: level.toSentryLevel(), | ||
| message: SentryMessage(message), | ||
| throwable: error, | ||
| extra: <String, Object>{ | ||
| if (object != null) 'LogRecord.object': object!, | ||
| 'LogRecord.sequenceNumber': sequenceNumber, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| extension LogLevelX on Level { | ||
| SentryLevel? toSentryLevel() { | ||
| return <Level, SentryLevel?>{ | ||
| Level.ALL: SentryLevel.debug, | ||
| Level.FINEST: SentryLevel.debug, | ||
| Level.FINER: SentryLevel.debug, | ||
| Level.FINE: SentryLevel.debug, | ||
| Level.CONFIG: SentryLevel.debug, | ||
| Level.INFO: SentryLevel.info, | ||
| Level.WARNING: SentryLevel.warning, | ||
| Level.SEVERE: SentryLevel.error, | ||
| Level.SHOUT: SentryLevel.fatal, | ||
| Level.OFF: null, | ||
| }[this]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:logging/logging.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
| import 'extension.dart'; | ||
|
|
||
| /// An [Integration] which listens to all messages of the | ||
| /// [logging](https://pub.dev/packages/logging) package. | ||
| class LoggingIntegration extends Integration<SentryOptions> { | ||
| /// Creates the [LoggingIntegration]. | ||
| /// | ||
| /// Setting [logExceptionAsEvent] to true (default) captures all | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// messages with errors as an [SentryEvent] instead of an [Breadcrumb]. | ||
| /// Setting [logExceptionAsEvent] to false captures everything as | ||
| /// [Breadcrumb]s. | ||
| LoggingIntegration({bool logExceptionAsEvent = true}) | ||
| : _logExceptionsAsEvents = logExceptionAsEvent; | ||
|
|
||
| final bool _logExceptionsAsEvents; | ||
| late StreamSubscription<LogRecord> _subscription; | ||
| late Hub _hub; | ||
|
|
||
| @override | ||
| FutureOr<void> call(Hub hub, SentryOptions options) { | ||
| _hub = hub; | ||
| _subscription = Logger.root.onRecord.listen( | ||
| _onLog, | ||
| onError: (Object error, StackTrace stackTrace) { | ||
| _hub.captureException(error, stackTrace: stackTrace); | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| ); | ||
| options.sdk.addIntegration('LoggingIntegration'); | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @override | ||
| Future<void> close() async { | ||
| await super.close(); | ||
| await _subscription.cancel(); | ||
| } | ||
|
|
||
| void _onLog(LogRecord record) { | ||
| // Everything is just logged as a breadcrumb | ||
| if (!_logExceptionsAsEvents) { | ||
| _hub.addBreadcrumb(record.toBreadcrumb()); | ||
| return; | ||
| } | ||
|
|
||
| // If a LogRecord contains an exception, it gets reported as an SentryEvent | ||
| if (record.error == null) { | ||
| _hub.addBreadcrumb(record.toBreadcrumb()); | ||
| } else { | ||
| _hub.captureEvent( | ||
| record.toEvent(), | ||
| stackTrace: record.stackTrace, | ||
| ); | ||
| } | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| name: sentry_logging | ||
| description: An integration which adds support for recording log from the logging package. | ||
| version: 1.0.0 | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| homepage: https://docs.sentry.io/platforms/flutter/ | ||
| repository: https://github.com/getsentry/sentry-dart | ||
|
|
||
| environment: | ||
| sdk: '>=2.14.4 <3.0.0' | ||
ueman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| dependencies: | ||
| logging: ^1.0.0 | ||
| sentry: ^6.1.0 | ||
|
|
||
| dev_dependencies: | ||
| lints: ^1.0.0 | ||
| test: ^1.16.0 | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import 'package:logging/logging.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
| import 'package:sentry_logging/sentry_logging.dart'; | ||
| import 'package:test/expect.dart'; | ||
| import 'package:test/scaffolding.dart'; | ||
|
|
||
| import 'mock_hub.dart'; | ||
|
|
||
| void main() { | ||
| late Fixture fixture; | ||
| setUp(() { | ||
| fixture = Fixture(); | ||
| }); | ||
|
|
||
| test('options.sdk.integrations contains $LoggingIntegration', () async { | ||
| final sut = fixture.createSut(); | ||
| await sut.call(fixture.hub, fixture.options); | ||
| await sut.close(); | ||
| expect( | ||
| fixture.options.sdk.integrations.contains('LoggingIntegration'), | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test('logger gets recorded', () async { | ||
| final sut = fixture.createSut(); | ||
| await sut.call(fixture.hub, fixture.options); | ||
|
|
||
| final log = Logger('FooBarLogger'); | ||
| log.warning( | ||
| 'A log message', | ||
| ); | ||
|
|
||
| expect(fixture.hub.events.length, 0); | ||
| expect(fixture.hub.breadcrumbs.length, 1); | ||
| final crumb = fixture.hub.breadcrumbs.first; | ||
| expect(crumb.level, SentryLevel.warning); | ||
| expect(crumb.message, 'A log message'); | ||
| expect(crumb.data, <String, dynamic>{ | ||
| 'LogRecord.loggerName': 'FooBarLogger', | ||
| 'LogRecord.sequenceNumber': 0, | ||
| }); | ||
| expect(crumb.timestamp, isNotNull); | ||
| expect(crumb.category, 'log'); | ||
| expect(crumb.type, 'debug'); | ||
| }); | ||
|
|
||
| test('exceptions is recorded as breadcrumb if logExceptionsAsEvents = false', | ||
| () async { | ||
| final sut = fixture.createSut(logExceptionsAsEvents: false); | ||
| await sut.call(fixture.hub, fixture.options); | ||
|
|
||
| final log = Logger('FooBarLogger'); | ||
| log.warning( | ||
| 'A log message', | ||
| Exception('foo bar'), | ||
| StackTrace.current, | ||
| ); | ||
| expect(fixture.hub.events.length, 0); | ||
| expect(fixture.hub.breadcrumbs.length, 1); | ||
| final crumb = fixture.hub.breadcrumbs.first; | ||
| expect(crumb.data?.length, 4); | ||
| }); | ||
|
|
||
| test('exceptions is recorded as event if logExceptionsAsEvents = true', | ||
| () async { | ||
| final sut = fixture.createSut(logExceptionsAsEvents: true); | ||
| await sut.call(fixture.hub, fixture.options); | ||
|
|
||
| final exception = Exception('foo bar'); | ||
| final stackTrace = StackTrace.current; | ||
|
|
||
| final log = Logger('FooBarLogger'); | ||
| log.warning( | ||
| 'A log message', | ||
| exception, | ||
| stackTrace, | ||
| ); | ||
| expect(fixture.hub.breadcrumbs.length, 0); | ||
| expect(fixture.hub.events.length, 1); | ||
| final event = fixture.hub.events.first.event; | ||
| expect(event.level, SentryLevel.warning); | ||
| expect(event.logger, 'FooBarLogger'); | ||
| expect(event.throwable, exception); | ||
| expect(event.extra?['LogRecord.sequenceNumber'], isNotNull); | ||
| expect(fixture.hub.events.first.stackTrace, stackTrace); | ||
| }); | ||
| } | ||
|
|
||
| class Fixture { | ||
| SentryOptions options = SentryOptions(dsn: fakeDsn); | ||
| MockHub hub = MockHub(); | ||
|
|
||
| LoggingIntegration createSut({bool logExceptionsAsEvents = true}) { | ||
| return LoggingIntegration(logExceptionAsEvent: logExceptionsAsEvents); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.