1
1
import { format } from 'winston' ;
2
2
import { pick , get , map } from 'lodash' ;
3
+ import { inspect } from 'util' ;
3
4
4
5
const errorWhiteListFields = [
5
6
'message' ,
6
7
'command.name' ,
7
8
] ;
8
9
10
+ const sanitizeStack = ( stack : any [ ] ) => {
11
+ try {
12
+ let sanitizedStack = stack ;
13
+
14
+ if ( stack && stack . length ) {
15
+ sanitizedStack = stack . map ( ( error ) => {
16
+ if ( error ?. name === 'AxiosError' ) {
17
+ return {
18
+ ...pick ( error , [ 'message' , 'name' , 'code' , 'stack' ] ) ,
19
+ response : error ?. response ?. data ,
20
+ } ;
21
+ }
22
+
23
+ return error ;
24
+ } ) ;
25
+ }
26
+
27
+ return inspect ( sanitizedStack ) ;
28
+ } catch ( e ) {
29
+ return e . stack ;
30
+ }
31
+ } ;
32
+
9
33
/**
10
34
* Get only whitelisted fields from logs when omitSensitiveData option enabled
11
35
*/
@@ -28,7 +52,7 @@ export const sensitiveDataFormatter = format((info, opts = {}) => {
28
52
29
53
return {
30
54
...info ,
31
- stack,
55
+ stack : sanitizeStack ( stack ) ,
32
56
} ;
33
57
} ) ;
34
58
@@ -38,7 +62,7 @@ export const jsonFormat = format.printf((info) => {
38
62
timestamp : new Date ( ) . toLocaleString ( ) ,
39
63
context : info . context ,
40
64
message : info . message ,
41
- stack : info . stack ,
65
+ stack : sanitizeStack ( info . stack ) ,
42
66
} ;
43
67
return JSON . stringify ( logData ) ;
44
68
} ) ;
@@ -49,6 +73,7 @@ export const prettyFormat = format.printf((info) => {
49
73
const {
50
74
level, context, message, stack,
51
75
} = info ;
52
- const logData = [ timestamp , `${ level } ` . toUpperCase ( ) , context , message , JSON . stringify ( { stack } ) ] ;
76
+
77
+ const logData = [ timestamp , `${ level } ` . toUpperCase ( ) , context , message , { stack : sanitizeStack ( stack ) } ] ;
53
78
return logData . join ( separator ) ;
54
79
} ) ;
0 commit comments