warn method
- String message, {
- Object? error,
- StackTrace? stackTrace,
- Map<
String, Object?> ? extras, - bool escalate = false,
override
Logs a warning for a potentially problematic situation.
Warning messages indicate issues that do not prevent operation but may require attention, such as fallback behaviour or recoverable errors.
The message describes the warning condition. The optional error and
stackTrace provide exception context. The extras map may include
additional context such as affected resources or retry counts. Set
escalate to true to also send this warning to error tracking (Sentry).
Example:
logger.warn(
'Token refresh response was malformed',
extras: {'endpoint': '/api/token/refresh'},
escalate: true,
);
Implementation
@override
void warn(String message, {Object? error, StackTrace? stackTrace, Map<String, Object?>? extras, bool escalate = false}) {
_breadcrumb(message, level: SentryLevel.warning, category: 'app.warn', extras: extras);
if (escalate) {
if (error != null) {
unawaited(
Sentry.captureException(
error,
stackTrace: stackTrace,
withScope: (Scope scope) {
scope.level = SentryLevel.warning;
scope.setContexts('details', <String, Object?>{'message': message, ...?extras});
},
),
);
} else {
unawaited(
Sentry.captureMessage(
message,
level: SentryLevel.warning,
withScope: (Scope scope) {
if (extras != null && extras.isNotEmpty) scope.setContexts('details', extras);
},
),
);
}
}
if (kDebugMode) {
debugPrint('⚠️ $message');
if (error != null) debugPrint('⚠️ error: $error');
if (stackTrace != null) debugPrint('⚠️ stack: $stackTrace');
}
}