@@ -14,7 +14,7 @@ export const logEnricherIntegration = (): Integration => {
1414 cacheLogContext ( ) . then (
1515 ( ) => {
1616 client . on ( 'beforeCaptureLog' , ( log : Log ) => {
17- processLog ( log ) ;
17+ processLog ( log , client ) ;
1818 } ) ;
1919 } ,
2020 reason => {
@@ -28,6 +28,25 @@ export const logEnricherIntegration = (): Integration => {
2828
2929let NativeCache : Record < string , unknown > | undefined = undefined ;
3030
31+ /**
32+ * Sets a log attribute if the value exists and the attribute key is not already present.
33+ *
34+ * @param logAttributes - The log attributes object to modify.
35+ * @param key - The attribute key to set.
36+ * @param value - The value to set (only sets if truthy and key not present).
37+ * @param setEvenIfPresent - Whether to set the attribute if it is present. Defaults to true.
38+ */
39+ function setLogAttribute (
40+ logAttributes : Record < string , unknown > ,
41+ key : string ,
42+ value : unknown ,
43+ setEvenIfPresent = true ,
44+ ) : void {
45+ if ( value && ( ! logAttributes [ key ] || setEvenIfPresent ) ) {
46+ logAttributes [ key ] = value ;
47+ }
48+ }
49+
3150async function cacheLogContext ( ) : Promise < void > {
3251 try {
3352 const response = await NATIVE . fetchNativeLogAttributes ( ) ;
@@ -52,16 +71,26 @@ async function cacheLogContext(): Promise<void> {
5271 return Promise . resolve ( ) ;
5372}
5473
55- function processLog ( log : Log ) : void {
74+ function processLog ( log : Log , client : ReactNativeClient ) : void {
5675 if ( NativeCache === undefined ) {
5776 return ;
5877 }
5978
60- log . attributes = log . attributes ?? { } ;
61- NativeCache . brand && ( log . attributes [ 'device.brand' ] = NativeCache . brand ) ;
62- NativeCache . model && ( log . attributes [ 'device.model' ] = NativeCache . model ) ;
63- NativeCache . family && ( log . attributes [ 'device.family' ] = NativeCache . family ) ;
64- NativeCache . os && ( log . attributes [ 'os.name' ] = NativeCache . os ) ;
65- NativeCache . version && ( log . attributes [ 'os.version' ] = NativeCache . version ) ;
66- NativeCache . release && ( log . attributes [ 'sentry.release' ] = NativeCache . release ) ;
79+ // Save log.attributes to a new variable
80+ const logAttributes = log . attributes ?? { } ;
81+
82+ // Use setLogAttribute with the variable instead of direct assignment
83+ setLogAttribute ( logAttributes , 'device.brand' , NativeCache . brand ) ;
84+ setLogAttribute ( logAttributes , 'device.model' , NativeCache . model ) ;
85+ setLogAttribute ( logAttributes , 'device.family' , NativeCache . family ) ;
86+ setLogAttribute ( logAttributes , 'os.name' , NativeCache . os ) ;
87+ setLogAttribute ( logAttributes , 'os.version' , NativeCache . version ) ;
88+ setLogAttribute ( logAttributes , 'sentry.release' , NativeCache . release ) ;
89+
90+ const replay = client . getIntegrationByName < Integration & { getReplayId : ( ) => string | null } > ( 'MobileReplay' ) ;
91+ setLogAttribute ( logAttributes , 'sentry.replay_id' , replay ?. getReplayId ( ) ) ;
92+
93+
94+ // Set log.attributes to the variable
95+ log . attributes = logAttributes ;
6796}
0 commit comments