@@ -6,15 +6,27 @@ import { playtypusUserIdStorage } from '../middlewares';
6
6
function addMessageToTraceSpan (
7
7
method : 'log' | 'error' | 'warn' ,
8
8
message : string ,
9
- args ? : unknown [ ] ,
9
+ args : unknown [ ] = [ ] ,
10
10
) {
11
11
const span = trace . getSpan ( context . active ( ) ) ;
12
- if ( span ) {
13
- span . addEvent ( method , {
14
- message,
15
- args : args ? args . map ( ( arg ) => JSON . stringify ( arg ) ) . join ( ',' ) : '' ,
16
- } ) ;
12
+ if ( ! span ) {
13
+ return ;
17
14
}
15
+
16
+ const data = args ;
17
+ const userId = playtypusUserIdStorage . getStore ( ) ;
18
+
19
+ if ( userId ) {
20
+ data . push ( { platypusUserId : userId } ) ;
21
+ }
22
+
23
+ span . addEvent ( method , {
24
+ message,
25
+ args :
26
+ data ?. length !== 0
27
+ ? data . map ( ( arg ) => JSON . stringify ( arg ) ) . join ( ',' )
28
+ : '' ,
29
+ } ) ;
18
30
}
19
31
20
32
/**
@@ -30,9 +42,7 @@ export const infoLogger = (
30
42
apiKey ?: string ,
31
43
...args : unknown [ ]
32
44
) : void => {
33
- const userId = playtypusUserIdStorage . getStore ( ) ;
34
-
35
- addMessageToTraceSpan ( 'log' , message , [ ...args , userId ] ) ;
45
+ addMessageToTraceSpan ( 'log' , message , [ ...args ] ) ;
36
46
37
47
logger ( console . info , source , message , apiKey , ...args ) ;
38
48
} ;
@@ -50,9 +60,7 @@ export const errorLogger = (
50
60
apiKey ?: string ,
51
61
...args : unknown [ ]
52
62
) : void => {
53
- const userId = playtypusUserIdStorage . getStore ( ) ;
54
-
55
- addMessageToTraceSpan ( 'error' , message , [ ...args , userId ] ) ;
63
+ addMessageToTraceSpan ( 'error' , message , [ ...args ] ) ;
56
64
57
65
logger ( console . error , source , message , apiKey , ...args ) ;
58
66
} ;
@@ -70,13 +78,10 @@ export const warnLogger = (
70
78
apiKey ?: string ,
71
79
...args : unknown [ ]
72
80
) : void => {
73
- const userId = playtypusUserIdStorage . getStore ( ) ;
74
-
75
- addMessageToTraceSpan ( 'warn' , message , [ ...args , userId ] ) ;
81
+ addMessageToTraceSpan ( 'warn' , message , [ ...args ] ) ;
76
82
77
83
logger ( console . warn , source , message , apiKey , ...args ) ;
78
84
} ;
79
-
80
85
const logger = (
81
86
logFn : ( message ?: any , ...optionalParams : any [ ] ) => void ,
82
87
source : string ,
@@ -86,33 +91,39 @@ const logger = (
86
91
) : void => {
87
92
// eslint-disable-next-line no-console
88
93
const anonymizedApiKey = apiKey ? anonymizeKey ( apiKey ) : undefined ;
89
- const userId = playtypusUserIdStorage . getStore ( ) ;
90
94
91
95
const formatedMessage = constructLogMessage (
92
96
anonymizedApiKey ? `[${ anonymizedApiKey } ]` : undefined ,
93
- userId ? `[${ userId } ]` : undefined ,
94
97
`[${ source } ]` ,
95
98
message ,
96
99
) ;
97
100
98
- if ( process . env . NODE_ENV == 'development' ) {
101
+ if ( process . env . NODE_ENV === 'development' ) {
99
102
logFn ( formatedMessage , ...args ) ;
100
- } else {
101
- logFn (
102
- JSON . stringify ( {
103
- message : formatedMessage ,
104
- data : { ...args , integrationUserId : userId } ,
105
- } ) ,
106
- ) ;
103
+ return ;
104
+ }
105
+
106
+ const data : Record < string , unknown > = { ...args } ;
107
+ const userId = playtypusUserIdStorage . getStore ( ) ;
108
+
109
+ if ( userId ) {
110
+ data . platypusUserId = userId ;
107
111
}
112
+
113
+ logFn (
114
+ JSON . stringify ( {
115
+ message : formatedMessage ,
116
+ data,
117
+ } ) ,
118
+ ) ;
108
119
} ;
109
120
110
121
const constructLogMessage = ( ...args : unknown [ ] ) : string =>
111
122
`${ args
112
123
. flat ( )
113
- . filter ( ( item ) => item != undefined )
124
+ . filter ( ( item ) => item !== undefined )
114
125
. map ( ( item : unknown ) => {
115
- if ( Array . isArray ( item ) && item . length == 0 ) return ;
126
+ if ( Array . isArray ( item ) && item . length === 0 ) return ;
116
127
return typeof item !== 'string' ? JSON . stringify ( item ) : item ;
117
128
} )
118
129
. join ( ' ' ) } `;
0 commit comments