@@ -187,4 +187,96 @@ describe('_INTERNAL_captureLog', () => {
187187    _INTERNAL_flushLogsBuffer ( client ) ; 
188188    expect ( mockSendEnvelope ) . not . toHaveBeenCalled ( ) ; 
189189  } ) ; 
190+ 
191+   it ( 'processes logs through beforeSendLog when provided' ,  ( )  =>  { 
192+     const  beforeSendLog  =  vi . fn ( ) . mockImplementation ( log  =>  ( { 
193+       ...log , 
194+       message : `Modified: ${ log . message }  , 
195+       attributes : {  ...log . attributes ,  processed : true  } , 
196+     } ) ) ; 
197+ 
198+     const  options  =  getDefaultTestClientOptions ( { 
199+       dsn : PUBLIC_DSN , 
200+       _experiments : {  enableLogs : true ,  beforeSendLog } , 
201+     } ) ; 
202+     const  client  =  new  TestClient ( options ) ; 
203+ 
204+     _INTERNAL_captureLog ( 
205+       { 
206+         level : 'info' , 
207+         message : 'original message' , 
208+         attributes : {  original : true  } , 
209+       } , 
210+       client , 
211+       undefined , 
212+     ) ; 
213+ 
214+     expect ( beforeSendLog ) . toHaveBeenCalledWith ( { 
215+       level : 'info' , 
216+       message : 'original message' , 
217+       attributes : {  original : true  } , 
218+     } ) ; 
219+ 
220+     const  logBuffer  =  _INTERNAL_getLogBuffer ( client ) ; 
221+     expect ( logBuffer ) . toBeDefined ( ) ; 
222+     expect ( logBuffer ?. [ 0 ] ) . toEqual ( 
223+       expect . objectContaining ( { 
224+         body : { 
225+           stringValue : 'Modified: original message' , 
226+         } , 
227+         attributes : expect . arrayContaining ( [ 
228+           expect . objectContaining ( {  key : 'processed' ,  value : {  boolValue : true  }  } ) , 
229+           expect . objectContaining ( {  key : 'original' ,  value : {  boolValue : true  }  } ) , 
230+         ] ) , 
231+       } ) , 
232+     ) ; 
233+   } ) ; 
234+ 
235+   it ( 'drops logs when beforeSendLog returns null' ,  ( )  =>  { 
236+     const  beforeSendLog  =  vi . fn ( ) . mockReturnValue ( null ) ; 
237+     const  recordDroppedEventSpy  =  vi . spyOn ( TestClient . prototype ,  'recordDroppedEvent' ) ; 
238+     const  loggerWarnSpy  =  vi . spyOn ( loggerModule . logger ,  'warn' ) . mockImplementation ( ( )  =>  undefined ) ; 
239+ 
240+     const  options  =  getDefaultTestClientOptions ( { 
241+       dsn : PUBLIC_DSN , 
242+       _experiments : {  enableLogs : true ,  beforeSendLog } , 
243+     } ) ; 
244+     const  client  =  new  TestClient ( options ) ; 
245+ 
246+     _INTERNAL_captureLog ( 
247+       { 
248+         level : 'info' , 
249+         message : 'test message' , 
250+       } , 
251+       client , 
252+       undefined , 
253+     ) ; 
254+ 
255+     expect ( beforeSendLog ) . toHaveBeenCalled ( ) ; 
256+     expect ( recordDroppedEventSpy ) . toHaveBeenCalledWith ( 'before_send' ,  'log_item' ,  1 ) ; 
257+     expect ( loggerWarnSpy ) . toHaveBeenCalledWith ( 'beforeSendLog returned null, log will not be captured.' ) ; 
258+     expect ( _INTERNAL_getLogBuffer ( client ) ) . toBeUndefined ( ) ; 
259+ 
260+     recordDroppedEventSpy . mockRestore ( ) ; 
261+     loggerWarnSpy . mockRestore ( ) ; 
262+   } ) ; 
263+ 
264+   it ( 'emits beforeCaptureLog and afterCaptureLog events' ,  ( )  =>  { 
265+     const  beforeCaptureLogSpy  =  vi . spyOn ( TestClient . prototype ,  'emit' ) ; 
266+     const  options  =  getDefaultTestClientOptions ( {  dsn : PUBLIC_DSN ,  _experiments : {  enableLogs : true  }  } ) ; 
267+     const  client  =  new  TestClient ( options ) ; 
268+ 
269+     _INTERNAL_captureLog ( 
270+       { 
271+         level : 'info' , 
272+         message : 'test message' , 
273+       } , 
274+       client , 
275+       undefined , 
276+     ) ; 
277+ 
278+     expect ( beforeCaptureLogSpy ) . toHaveBeenCalledWith ( 'beforeCaptureLog' ,  log ) ; 
279+     expect ( beforeCaptureLogSpy ) . toHaveBeenCalledWith ( 'afterCaptureLog' ,  log ) ; 
280+     beforeCaptureLogSpy . mockRestore ( ) ; 
281+   } ) ; 
190282} ) ; 
0 commit comments