Skip to content

Commit e843899

Browse files
denrasebuenaflor
andauthored
Add LoggingIntegration support for SentryLog (#3050)
* Add `LoggingIntegration` support for `SenryLog` * add cl entry * format * ignore override * don’t add error/stacktrace attributes * fix cl * Map finer and finest to trace * change min log level to info * Update docs * update ctor docs * handle custom levels * fix cl * combile defined and custom value checks * fix cl * Update CHANGELOG.md --------- Co-authored-by: Giancarlo Buenaflor <[email protected]> Co-authored-by: Giancarlo Buenaflor <[email protected]>
1 parent e395fa8 commit e843899

File tree

3 files changed

+397
-4
lines changed

3 files changed

+397
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Changelog
22

3-
# Unreleased
3+
## Unreleased
44

55
### Features
66

77
- Report Flutter framework feature flags ([#2991](https://github.com/getsentry/sentry-dart/pull/2991))
88
- Search for feature flags that are prefixed with `flutter:*`
99
- This works on Flutter builds that include [this PR](https://github.com/flutter/flutter/pull/171545)
10+
- Add `LoggingIntegration` support for `SentryLog` ([#3050](https://github.com/getsentry/sentry-dart/pull/3050))
1011

1112
### Dependencies
1213

logging/lib/src/logging_integration.dart

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,48 @@ import 'version.dart';
1111
class LoggingIntegration implements Integration<SentryOptions> {
1212
/// Creates the [LoggingIntegration].
1313
///
14-
/// All log events equal or higher than [minBreadcrumbLevel] are recorded as a
14+
/// - All log events equal or higher than [minBreadcrumbLevel] are recorded as a
1515
/// [Breadcrumb].
16-
/// All log events equal or higher than [minEventLevel] are recorded as a
16+
/// - All log events equal or higher than [minEventLevel] are recorded as a
1717
/// [SentryEvent].
18+
/// - All log events equal or higher than [minSentryLogLevel] are logged to
19+
/// Sentry, if [SentryOptions.enableLogs] is true.
20+
///
21+
/// Log levels are mapped to the following Sentry log levels methods:
22+
///
23+
/// | Dart Log Level | Sentry Log Level |
24+
/// |-----------------------|------------------|
25+
/// | SHOUT, SEVERE | error |
26+
/// | WARNING | warn |
27+
/// | INFO | info |
28+
/// | CONFIG, FINE, ALL | debug |
29+
/// | FINER, FINEST | trace |
30+
///
31+
/// Custom log levels are mapped based on their numeric values:
32+
/// - >= 1000 → error
33+
/// - >= 900 → warn
34+
/// - >= 800 → info
35+
/// - >= 700 || 500 || 0 → debug
36+
/// - < 700 → trace
1837
LoggingIntegration({
1938
Level minBreadcrumbLevel = Level.INFO,
2039
Level minEventLevel = Level.SEVERE,
40+
Level minSentryLogLevel = Level.INFO,
2141
}) : _minBreadcrumbLevel = minBreadcrumbLevel,
22-
_minEventLevel = minEventLevel;
42+
_minEventLevel = minEventLevel,
43+
_minSentryLogLevel = minSentryLogLevel;
2344

2445
final Level _minBreadcrumbLevel;
2546
final Level _minEventLevel;
47+
final Level _minSentryLogLevel;
2648
late StreamSubscription<LogRecord> _subscription;
2749
late Hub _hub;
50+
late SentryOptions _options;
2851

2952
@override
3053
void call(Hub hub, SentryOptions options) {
3154
_hub = hub;
55+
_options = options;
3256
_subscription = Logger.root.onRecord.listen(
3357
_onLog,
3458
onError: (Object error, StackTrace stackTrace) async {
@@ -68,5 +92,34 @@ class LoggingIntegration implements Integration<SentryOptions> {
6892
hint: Hint.withMap({TypeCheckHint.record: record}),
6993
);
7094
}
95+
96+
if (_options.enableLogs && _isLoggable(record.level, _minSentryLogLevel)) {
97+
final attributes = {
98+
'loggerName': SentryLogAttribute.string(record.loggerName),
99+
'sequenceNumber': SentryLogAttribute.int(record.sequenceNumber),
100+
'time': SentryLogAttribute.int(record.time.millisecondsSinceEpoch),
101+
};
102+
103+
// Map log levels based on value ranges
104+
final levelValue = record.level.value;
105+
if (levelValue >= Level.SEVERE.value) {
106+
// >= 1000 → error
107+
await _options.logger.error(record.message, attributes: attributes);
108+
} else if (levelValue >= Level.WARNING.value) {
109+
// >= 900 → warn
110+
await _options.logger.warn(record.message, attributes: attributes);
111+
} else if (levelValue >= Level.INFO.value) {
112+
// >= 800 → info
113+
await _options.logger.info(record.message, attributes: attributes);
114+
} else if (levelValue >= Level.CONFIG.value ||
115+
levelValue == Level.FINE.value ||
116+
levelValue == Level.ALL.value) {
117+
// >= 700 || 500 || 0 → debug
118+
await _options.logger.debug(record.message, attributes: attributes);
119+
} else {
120+
// < 700 → trace
121+
await _options.logger.trace(record.message, attributes: attributes);
122+
}
123+
}
71124
}
72125
}

0 commit comments

Comments
 (0)