77} from '../../../src/logs' ;
88import { TestClient , getDefaultTestClientOptions } from '../../mocks/client' ;
99import * as loggerModule from '../../../src/utils-hoist/logger' ;
10- import { Scope } from '../../../src' ;
11- import { parameterize } from '../../../src/utils/parameterize ' ;
10+ import { Scope , fmt } from '../../../src' ;
11+ import type { Log } from '../../../src/types-hoist/log ' ;
1212
1313const PUBLIC_DSN = 'https://username@domain/123' ;
1414
@@ -194,7 +194,7 @@ describe('_INTERNAL_captureLog', () => {
194194 const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , _experiments : { enableLogs : true } } ) ;
195195 const client = new TestClient ( options ) ;
196196
197- const parameterizedMessage = parameterize `Hello ${ 'John' } , welcome to ${ 'Sentry' } ` ;
197+ const parameterizedMessage = fmt `Hello ${ 'John' } , welcome to ${ 'Sentry' } ` ;
198198
199199 _INTERNAL_captureLog ( { level : 'info' , message : parameterizedMessage } , client , undefined ) ;
200200
@@ -216,4 +216,94 @@ describe('_INTERNAL_captureLog', () => {
216216 ] ) ,
217217 ) ;
218218 } ) ;
219+
220+ it ( 'processes logs through beforeSendLog when provided' , ( ) => {
221+ const beforeSendLog = vi . fn ( ) . mockImplementation ( log => ( {
222+ ...log ,
223+ message : `Modified: ${ log . message } ` ,
224+ attributes : { ...log . attributes , processed : true } ,
225+ } ) ) ;
226+
227+ const options = getDefaultTestClientOptions ( {
228+ dsn : PUBLIC_DSN ,
229+ _experiments : { enableLogs : true , beforeSendLog } ,
230+ } ) ;
231+ const client = new TestClient ( options ) ;
232+
233+ _INTERNAL_captureLog (
234+ {
235+ level : 'info' ,
236+ message : 'original message' ,
237+ attributes : { original : true } ,
238+ } ,
239+ client ,
240+ undefined ,
241+ ) ;
242+
243+ expect ( beforeSendLog ) . toHaveBeenCalledWith ( {
244+ level : 'info' ,
245+ message : 'original message' ,
246+ attributes : { original : true } ,
247+ } ) ;
248+
249+ const logBuffer = _INTERNAL_getLogBuffer ( client ) ;
250+ expect ( logBuffer ) . toBeDefined ( ) ;
251+ expect ( logBuffer ?. [ 0 ] ) . toEqual (
252+ expect . objectContaining ( {
253+ body : {
254+ stringValue : 'Modified: original message' ,
255+ } ,
256+ attributes : expect . arrayContaining ( [
257+ expect . objectContaining ( { key : 'processed' , value : { boolValue : true } } ) ,
258+ expect . objectContaining ( { key : 'original' , value : { boolValue : true } } ) ,
259+ ] ) ,
260+ } ) ,
261+ ) ;
262+ } ) ;
263+
264+ it ( 'drops logs when beforeSendLog returns null' , ( ) => {
265+ const beforeSendLog = vi . fn ( ) . mockReturnValue ( null ) ;
266+ const recordDroppedEventSpy = vi . spyOn ( TestClient . prototype , 'recordDroppedEvent' ) ;
267+ const loggerWarnSpy = vi . spyOn ( loggerModule . logger , 'warn' ) . mockImplementation ( ( ) => undefined ) ;
268+
269+ const options = getDefaultTestClientOptions ( {
270+ dsn : PUBLIC_DSN ,
271+ _experiments : { enableLogs : true , beforeSendLog } ,
272+ } ) ;
273+ const client = new TestClient ( options ) ;
274+
275+ _INTERNAL_captureLog (
276+ {
277+ level : 'info' ,
278+ message : 'test message' ,
279+ } ,
280+ client ,
281+ undefined ,
282+ ) ;
283+
284+ expect ( beforeSendLog ) . toHaveBeenCalled ( ) ;
285+ expect ( recordDroppedEventSpy ) . toHaveBeenCalledWith ( 'before_send' , 'log_item' , 1 ) ;
286+ expect ( loggerWarnSpy ) . toHaveBeenCalledWith ( 'beforeSendLog returned null, log will not be captured.' ) ;
287+ expect ( _INTERNAL_getLogBuffer ( client ) ) . toBeUndefined ( ) ;
288+
289+ recordDroppedEventSpy . mockRestore ( ) ;
290+ loggerWarnSpy . mockRestore ( ) ;
291+ } ) ;
292+
293+ it ( 'emits beforeCaptureLog and afterCaptureLog events' , ( ) => {
294+ const beforeCaptureLogSpy = vi . spyOn ( TestClient . prototype , 'emit' ) ;
295+ const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , _experiments : { enableLogs : true } } ) ;
296+ const client = new TestClient ( options ) ;
297+
298+ const log : Log = {
299+ level : 'info' ,
300+ message : 'test message' ,
301+ } ;
302+
303+ _INTERNAL_captureLog ( log , client , undefined ) ;
304+
305+ expect ( beforeCaptureLogSpy ) . toHaveBeenCalledWith ( 'beforeCaptureLog' , log ) ;
306+ expect ( beforeCaptureLogSpy ) . toHaveBeenCalledWith ( 'afterCaptureLog' , log ) ;
307+ beforeCaptureLogSpy . mockRestore ( ) ;
308+ } ) ;
219309} ) ;
0 commit comments