Skip to content

Commit 417dfb6

Browse files
author
arthosofteq
committed
fix logs circular dependency
1 parent 28a7350 commit 417dfb6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

redisinsight/api/src/utils/logsFormatter.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import { format } from 'winston';
22
import { pick, get, map } from 'lodash';
3+
import { inspect } from 'util';
34

45
const errorWhiteListFields = [
56
'message',
67
'command.name',
78
];
89

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+
933
/**
1034
* Get only whitelisted fields from logs when omitSensitiveData option enabled
1135
*/
@@ -28,7 +52,7 @@ export const sensitiveDataFormatter = format((info, opts = {}) => {
2852

2953
return {
3054
...info,
31-
stack,
55+
stack: sanitizeStack(stack),
3256
};
3357
});
3458

@@ -38,7 +62,7 @@ export const jsonFormat = format.printf((info) => {
3862
timestamp: new Date().toLocaleString(),
3963
context: info.context,
4064
message: info.message,
41-
stack: info.stack,
65+
stack: sanitizeStack(info.stack),
4266
};
4367
return JSON.stringify(logData);
4468
});
@@ -49,6 +73,7 @@ export const prettyFormat = format.printf((info) => {
4973
const {
5074
level, context, message, stack,
5175
} = info;
52-
const logData = [timestamp, `${level}`.toUpperCase(), context, message, JSON.stringify({ stack })];
76+
77+
const logData = [timestamp, `${level}`.toUpperCase(), context, message, { stack: sanitizeStack(stack) }];
5378
return logData.join(separator);
5479
});

0 commit comments

Comments
 (0)