11import { describe , expect , it , vi } from 'vitest' ;
22import { fmt , Scope } from '../../../src' ;
3- import {
4- _INTERNAL_captureLog ,
5- _INTERNAL_flushLogsBuffer ,
6- _INTERNAL_getLogBuffer ,
7- logAttributeToSerializedLogAttribute ,
8- } from '../../../src/logs/internal' ;
3+ import { _INTERNAL_captureLog , _INTERNAL_flushLogsBuffer , _INTERNAL_getLogBuffer } from '../../../src/logs/internal' ;
94import type { Log } from '../../../src/types-hoist/log' ;
105import * as loggerModule from '../../../src/utils/debug-logger' ;
116import { getDefaultTestClientOptions , TestClient } from '../../mocks/client' ;
127
138const PUBLIC_DSN = 'https://username@domain/123' ;
149
15- describe ( 'logAttributeToSerializedLogAttribute' , ( ) => {
16- it ( 'serializes integer values' , ( ) => {
17- const result = logAttributeToSerializedLogAttribute ( 42 ) ;
18- expect ( result ) . toEqual ( {
19- value : 42 ,
20- type : 'integer' ,
21- } ) ;
22- } ) ;
23-
24- it ( 'serializes double values' , ( ) => {
25- const result = logAttributeToSerializedLogAttribute ( 42.34 ) ;
26- expect ( result ) . toEqual ( {
27- value : 42.34 ,
28- type : 'double' ,
29- } ) ;
30- } ) ;
31-
32- it ( 'serializes boolean values' , ( ) => {
33- const result = logAttributeToSerializedLogAttribute ( true ) ;
34- expect ( result ) . toEqual ( {
35- value : true ,
36- type : 'boolean' ,
37- } ) ;
38- } ) ;
39-
40- it ( 'serializes string values' , ( ) => {
41- const result = logAttributeToSerializedLogAttribute ( 'username' ) ;
42- expect ( result ) . toEqual ( {
43- value : 'username' ,
44- type : 'string' ,
45- } ) ;
46- } ) ;
47-
48- it ( 'serializes object values as JSON strings' , ( ) => {
49- const obj = { name : 'John' , age : 30 } ;
50- const result = logAttributeToSerializedLogAttribute ( obj ) ;
51- expect ( result ) . toEqual ( {
52- value : JSON . stringify ( obj ) ,
53- type : 'string' ,
54- } ) ;
55- } ) ;
56-
57- it ( 'serializes array values as JSON strings' , ( ) => {
58- const array = [ 1 , 2 , 3 , 'test' ] ;
59- const result = logAttributeToSerializedLogAttribute ( array ) ;
60- expect ( result ) . toEqual ( {
61- value : JSON . stringify ( array ) ,
62- type : 'string' ,
63- } ) ;
64- } ) ;
65-
66- it ( 'serializes undefined values as empty strings' , ( ) => {
67- const result = logAttributeToSerializedLogAttribute ( undefined ) ;
68- expect ( result ) . toEqual ( {
69- value : '' ,
70- type : 'string' ,
71- } ) ;
72- } ) ;
73-
74- it ( 'serializes null values as JSON strings' , ( ) => {
75- const result = logAttributeToSerializedLogAttribute ( null ) ;
76- expect ( result ) . toEqual ( {
77- value : 'null' ,
78- type : 'string' ,
79- } ) ;
80- } ) ;
81- } ) ;
82-
8310describe ( '_INTERNAL_captureLog' , ( ) => {
8411 it ( 'captures and sends logs' , ( ) => {
8512 const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , enableLogs : true } ) ;
@@ -215,31 +142,92 @@ describe('_INTERNAL_captureLog', () => {
215142 ) ;
216143 } ) ;
217144
218- it ( 'includes custom attributes in log' , ( ) => {
219- const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , enableLogs : true } ) ;
220- const client = new TestClient ( options ) ;
221- const scope = new Scope ( ) ;
222- scope . setClient ( client ) ;
145+ describe ( 'attributes' , ( ) => {
146+ it ( 'includes custom attributes in log' , ( ) => {
147+ const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , enableLogs : true } ) ;
148+ const client = new TestClient ( options ) ;
149+ const scope = new Scope ( ) ;
150+ scope . setClient ( client ) ;
223151
224- _INTERNAL_captureLog (
225- {
226- level : 'info' ,
227- message : 'test log with custom attributes' ,
228- attributes : { userId : '123' , component : 'auth' } ,
229- } ,
230- scope ,
231- ) ;
152+ _INTERNAL_captureLog (
153+ {
154+ level : 'info' ,
155+ message : 'test log with custom attributes' ,
156+ attributes : { userId : '123' , component : 'auth' } ,
157+ } ,
158+ scope ,
159+ ) ;
232160
233- const logAttributes = _INTERNAL_getLogBuffer ( client ) ?. [ 0 ] ?. attributes ;
234- expect ( logAttributes ) . toEqual ( {
235- userId : {
236- value : '123' ,
237- type : 'string' ,
238- } ,
239- component : {
240- value : 'auth' ,
241- type : 'string' ,
242- } ,
161+ const logAttributes = _INTERNAL_getLogBuffer ( client ) ?. [ 0 ] ?. attributes ;
162+ expect ( logAttributes ) . toEqual ( {
163+ userId : {
164+ value : '123' ,
165+ type : 'string' ,
166+ } ,
167+ component : {
168+ value : 'auth' ,
169+ type : 'string' ,
170+ } ,
171+ } ) ;
172+ } ) ;
173+
174+ it ( 'applies scope attributes attributes to log' , ( ) => {
175+ const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , enableLogs : true } ) ;
176+ const client = new TestClient ( options ) ;
177+ const scope = new Scope ( ) ;
178+ scope . setClient ( client ) ;
179+
180+ scope . setAttribute ( 'scope_1' , 'attribute_value' ) ;
181+ scope . setAttribute ( 'scope_2' , { value : 143.5 , type : 'double' , unit : 'bytes' } ) ;
182+ scope . setAttributes ( {
183+ scope_3 : true ,
184+ scope_4 : [ 1 , 2 , 3 ] ,
185+ scope_5 : { value : [ true , false , true ] , type : 'boolean[]' , unit : 's' } ,
186+ } ) ;
187+
188+ _INTERNAL_captureLog (
189+ {
190+ level : 'info' ,
191+ message : 'test log with custom attributes' ,
192+ attributes : { userId : '123' , component : 'auth' } ,
193+ } ,
194+ scope ,
195+ ) ;
196+
197+ const logAttributes = _INTERNAL_getLogBuffer ( client ) ?. [ 0 ] ?. attributes ;
198+
199+ expect ( logAttributes ) . toEqual ( {
200+ userId : {
201+ value : '123' ,
202+ type : 'string' ,
203+ } ,
204+ component : {
205+ value : 'auth' ,
206+ type : 'string' ,
207+ } ,
208+ scope_1 : {
209+ type : 'string' ,
210+ value : 'attribute_value' ,
211+ } ,
212+ scope_2 : {
213+ type : 'double' ,
214+ unit : 'bytes' ,
215+ value : 143.5 ,
216+ } ,
217+ scope_3 : {
218+ type : 'boolean' ,
219+ value : true ,
220+ } ,
221+ scope_4 : {
222+ type : 'integer[]' ,
223+ value : [ 1 , 2 , 3 ] ,
224+ } ,
225+ scope_5 : {
226+ type : 'boolean[]' ,
227+ value : [ true , false , true ] ,
228+ unit : 's' ,
229+ } ,
230+ } ) ;
243231 } ) ;
244232 } ) ;
245233
@@ -326,6 +314,9 @@ describe('_INTERNAL_captureLog', () => {
326314 const scope = new Scope ( ) ;
327315 scope . setClient ( client ) ;
328316
317+ scope . setAttribute ( 'scope_1' , 'attribute_value' ) ;
318+ scope . setAttribute ( 'scope_2' , { value : 143.5 , type : 'double' , unit : 'bytes' } ) ;
319+
329320 _INTERNAL_captureLog (
330321 {
331322 level : 'info' ,
@@ -338,7 +329,12 @@ describe('_INTERNAL_captureLog', () => {
338329 expect ( beforeSendLog ) . toHaveBeenCalledWith ( {
339330 level : 'info' ,
340331 message : 'original message' ,
341- attributes : { original : true } ,
332+ attributes : {
333+ original : true ,
334+ // scope attributes should already be applied prior to beforeSendLog
335+ scope_1 : 'attribute_value' ,
336+ scope_2 : { value : 143.5 , type : 'double' , unit : 'bytes' } ,
337+ } ,
342338 } ) ;
343339
344340 const logBuffer = _INTERNAL_getLogBuffer ( client ) ;
0 commit comments