11using System ;
22using Sentry . Extensibility ;
3+ using Sentry . Infrastructure ;
34using Sentry . Integrations ;
45using UnityEngine ;
56
@@ -12,26 +13,28 @@ namespace Sentry.Unity.Integrations;
1213/// </summary>
1314internal class UnityApplicationLoggingIntegration : ISdkIntegration
1415{
15- private readonly IApplication _application ;
1616 private readonly bool _captureExceptions ;
17+ private readonly IApplication _application ;
18+ private readonly ISystemClock _clock ;
1719
1820 private ErrorTimeDebounce _errorTimeDebounce = null ! ; // Set in Register
1921 private LogTimeDebounce _logTimeDebounce = null ! ; // Set in Register
2022 private WarningTimeDebounce _warningTimeDebounce = null ! ; // Set in Register
2123
22- private IHub ? _hub ;
24+ private IHub _hub = null ! ; // Set in Register
2325 private SentryUnityOptions _options = null ! ; // Set in Register
2426
25- internal UnityApplicationLoggingIntegration ( bool captureExceptions = false , IApplication ? application = null )
27+ internal UnityApplicationLoggingIntegration ( bool captureExceptions = false , IApplication ? application = null , ISystemClock ? clock = null )
2628 {
2729 _captureExceptions = captureExceptions ;
2830 _application = application ?? ApplicationAdapter . Instance ;
31+ _clock = clock ?? SystemClock . Clock ;
2932 }
3033
3134 public void Register ( IHub hub , SentryOptions sentryOptions )
3235 {
33- _hub = hub ;
34- // This should never throw
36+ // These should never throw but in case they do...
37+ _hub = hub ?? throw new ArgumentException ( "Hub is null." ) ;
3538 _options = sentryOptions as SentryUnityOptions ?? throw new ArgumentException ( "Options is not of type 'SentryUnityOptions'." ) ;
3639
3740 _logTimeDebounce = new LogTimeDebounce ( _options . DebounceTimeLog ) ;
@@ -44,11 +47,6 @@ public void Register(IHub hub, SentryOptions sentryOptions)
4447
4548 internal void OnLogMessageReceived ( string message , string stacktrace , LogType logType )
4649 {
47- if ( _hub is null )
48- {
49- return ;
50- }
51-
5250 // We're not capturing the SDK's own logs
5351 if ( message . StartsWith ( UnityLogger . LogTag ) )
5452 {
@@ -64,6 +62,7 @@ internal void OnLogMessageReceived(string message, string stacktrace, LogType lo
6462 ProcessException ( message , stacktrace , logType ) ;
6563 ProcessError ( message , stacktrace , logType ) ;
6664 ProcessBreadcrumbs ( message , logType ) ;
65+ ProcessStructuredLog ( message , logType ) ;
6766 }
6867
6968 private bool IsGettingDebounced ( LogType logType )
@@ -92,7 +91,7 @@ private void ProcessException(string message, string stacktrace, LogType logType
9291 _options . LogDebug ( "Exception capture has been enabled. Capturing exception through '{0}'." , nameof ( UnityApplicationLoggingIntegration ) ) ;
9392
9493 var evt = UnityLogEventFactory . CreateExceptionEvent ( message , stacktrace , false , _options ) ;
95- _hub ? . CaptureEvent ( evt ) ;
94+ _hub . CaptureEvent ( evt ) ;
9695 }
9796 }
9897
@@ -108,11 +107,11 @@ private void ProcessError(string message, string stacktrace, LogType logType)
108107 if ( _options . AttachStacktrace && ! string . IsNullOrEmpty ( stacktrace ) )
109108 {
110109 var evt = UnityLogEventFactory . CreateMessageEvent ( message , stacktrace , SentryLevel . Error , _options ) ;
111- _hub ? . CaptureEvent ( evt ) ;
110+ _hub . CaptureEvent ( evt ) ;
112111 }
113112 else
114113 {
115- _hub ? . CaptureMessage ( message , level : SentryLevel . Error ) ;
114+ _hub . CaptureMessage ( message , level : SentryLevel . Error ) ;
116115 }
117116 }
118117
@@ -133,8 +132,26 @@ private void ProcessBreadcrumbs(string message, LogType logType)
133132 if ( _options . AddBreadcrumbsForLogType . TryGetValue ( logType , out var value ) && value )
134133 {
135134 _options . LogDebug ( "Adding breadcrumb for log message of type: {0}" , logType ) ;
136- _hub ? . AddBreadcrumb ( message : message , category : "unity.logger" , level : ToBreadcrumbLevel ( logType ) ) ;
135+ _hub . AddBreadcrumb ( message : message , category : "unity.logger" , level : ToBreadcrumbLevel ( logType ) ) ;
136+ }
137+ }
138+
139+ private void ProcessStructuredLog ( string message , LogType logType )
140+ {
141+ if ( ! _options . Experimental . EnableLogs || ! _options . Experimental . CaptureStructuredLogsForLogType . TryGetValue ( logType , out var captureLog ) || ! captureLog )
142+ {
143+ return ;
137144 }
145+
146+ _options . LogDebug ( "Capturing structured log message of type '{0}'." , logType ) ;
147+
148+ SentryLog . GetTraceIdAndSpanId ( _hub , out var traceId , out var spanId ) ;
149+ SentryLog log = new ( _clock . GetUtcNow ( ) , traceId , ToLogLevel ( logType ) , message ) { ParentSpanId = spanId } ;
150+
151+ log . SetDefaultAttributes ( _options , UnitySdkInfo . Sdk ) ;
152+ log . SetOrigin ( "auto.log.unity" ) ;
153+
154+ _hub . Logger . CaptureLog ( log ) ;
138155 }
139156
140157 private void OnQuitting ( ) => _application . LogMessageReceived -= OnLogMessageReceived ;
@@ -149,4 +166,15 @@ private static BreadcrumbLevel ToBreadcrumbLevel(LogType logType)
149166 LogType . Warning => BreadcrumbLevel . Warning ,
150167 _ => BreadcrumbLevel . Info
151168 } ;
169+
170+ private static SentryLogLevel ToLogLevel ( LogType logType )
171+ => logType switch
172+ {
173+ LogType . Assert => SentryLogLevel . Error ,
174+ LogType . Error => SentryLogLevel . Error ,
175+ LogType . Exception => SentryLogLevel . Error ,
176+ LogType . Log => SentryLogLevel . Info ,
177+ LogType . Warning => SentryLogLevel . Warning ,
178+ _ => SentryLogLevel . Info
179+ } ;
152180}
0 commit comments