@@ -27,6 +27,7 @@ import 'type_check_hint.dart';
2727import 'utils/isolate_utils.dart' ;
2828import 'utils/regex_utils.dart' ;
2929import 'utils/stacktrace_utils.dart' ;
30+ import 'sentry_log_batcher.dart' ;
3031import 'version.dart' ;
3132
3233/// Default value for [SentryUser.ipAddress] . It gets set when an event does not have
@@ -75,6 +76,9 @@ class SentryClient {
7576 if (enableFlutterSpotlight) {
7677 options.transport = SpotlightHttpTransport (options, options.transport);
7778 }
79+ if (options.enableLogs) {
80+ options.logBatcher = SentryLogBatcher (options);
81+ }
7882 return SentryClient ._(options);
7983 }
8084
@@ -485,6 +489,73 @@ class SentryClient {
485489 );
486490 }
487491
492+ @internal
493+ Future <void > captureLog (
494+ SentryLog log, {
495+ Scope ? scope,
496+ }) async {
497+ if (! _options.enableLogs) {
498+ return ;
499+ }
500+
501+ log.attributes['sentry.sdk.name' ] = SentryLogAttribute .string (
502+ _options.sdk.name,
503+ );
504+ log.attributes['sentry.sdk.version' ] = SentryLogAttribute .string (
505+ _options.sdk.version,
506+ );
507+ final environment = _options.environment;
508+ if (environment != null ) {
509+ log.attributes['sentry.environment' ] = SentryLogAttribute .string (
510+ environment,
511+ );
512+ }
513+ final release = _options.release;
514+ if (release != null ) {
515+ log.attributes['sentry.release' ] = SentryLogAttribute .string (
516+ release,
517+ );
518+ }
519+
520+ final propagationContext = scope? .propagationContext;
521+ if (propagationContext != null ) {
522+ log.traceId = propagationContext.traceId;
523+ }
524+ final span = scope? .span;
525+ if (span != null ) {
526+ log.attributes['sentry.trace.parent_span_id' ] = SentryLogAttribute .string (
527+ span.context.spanId.toString (),
528+ );
529+ }
530+
531+ final beforeSendLog = _options.beforeSendLog;
532+ SentryLog ? processedLog = log;
533+ if (beforeSendLog != null ) {
534+ try {
535+ final callbackResult = beforeSendLog (log);
536+
537+ if (callbackResult is Future <SentryLog ?>) {
538+ processedLog = await callbackResult;
539+ } else {
540+ processedLog = callbackResult;
541+ }
542+ } catch (exception, stackTrace) {
543+ _options.logger (
544+ SentryLevel .error,
545+ 'The beforeSendLog callback threw an exception' ,
546+ exception: exception,
547+ stackTrace: stackTrace,
548+ );
549+ if (_options.automatedTestMode) {
550+ rethrow ;
551+ }
552+ }
553+ }
554+ if (processedLog != null ) {
555+ _options.logBatcher.addLog (processedLog);
556+ }
557+ }
558+
488559 void close () {
489560 _options.httpClient.close ();
490561 }
0 commit comments