Skip to content

Commit 4c534b7

Browse files
committed
CR-277: Clone optional params
1 parent 0397bb9 commit 4c534b7

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

redisinsight/api/src/common/logger/app-logger.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LoggerService, Injectable } from '@nestjs/common';
22
import { WinstonModule, WinstonModuleOptions } from 'nest-winston';
3-
import { isString } from 'lodash';
3+
import { cloneDeep, isString } from 'lodash';
44
import { ClientMetadata, SessionMetadata } from 'src/common/models';
55

66
type LogMeta = object;
@@ -15,14 +15,6 @@ type ErrorOrMeta = Error | LogMeta | string | ClientMetadata | SessionMetadata;
1515
export class AppLogger implements LoggerService {
1616
private readonly logger: ReturnType<typeof WinstonModule.createLogger>;
1717

18-
static startupContexts = [
19-
'InstanceLoader',
20-
'RoutesResolver',
21-
'RouterExplorer',
22-
'WebSocketsController',
23-
'NestFactory',
24-
];
25-
2618
constructor(loggerConfig: WinstonModuleOptions) {
2719
this.logger = WinstonModule.createLogger(loggerConfig);
2820
}
@@ -34,14 +26,14 @@ export class AppLogger implements LoggerService {
3426
* Note: args array might be mutated
3527
* @param args
3628
*/
37-
static getContext(args: ErrorOrMeta[] = []): string {
29+
static getContext(args: ErrorOrMeta[] = []) {
3830
const lastArg = args?.[args.length - 1];
3931

4032
if (isString(lastArg)) {
4133
return args.pop() as string;
4234
}
4335

44-
return 'TODO: generic name or null?';
36+
return null;
4537
}
4638

4739
/**
@@ -52,7 +44,7 @@ export class AppLogger implements LoggerService {
5244
*/
5345
static getError(args: ErrorOrMeta[] = []): void | {} {
5446
let error = null;
55-
const index = args.findIndex((arg) => (arg instanceof Error));
47+
const index = args.findIndex((arg) => arg instanceof Error);
5648
if (index > -1) {
5749
[error] = args.splice(index, 1);
5850
}
@@ -77,13 +69,18 @@ export class AppLogger implements LoggerService {
7769
* @param args
7870
*/
7971
static getUserMetadata(args: ErrorOrMeta[] = []): {
80-
clientMetadata?: Partial<ClientMetadata>,
81-
sessionMetadata?: SessionMetadata,
72+
clientMetadata?: Partial<ClientMetadata>;
73+
sessionMetadata?: SessionMetadata;
8274
} {
8375
// check for client metadata in args
84-
const clientMetadataIndex = args.findIndex((arg) => (arg instanceof ClientMetadata));
76+
const clientMetadataIndex = args.findIndex(
77+
(arg) => arg instanceof ClientMetadata,
78+
);
8579
if (clientMetadataIndex > -1) {
86-
const [clientMetadata] = args.splice(clientMetadataIndex, 1) as ClientMetadata[];
80+
const [clientMetadata] = args.splice(
81+
clientMetadataIndex,
82+
1,
83+
) as ClientMetadata[];
8784
return {
8885
clientMetadata: {
8986
...clientMetadata,
@@ -94,9 +91,14 @@ export class AppLogger implements LoggerService {
9491
}
9592

9693
// check for session metadata in args
97-
const sessionMetadataIndex = args.findIndex((arg) => (arg instanceof SessionMetadata));
94+
const sessionMetadataIndex = args.findIndex(
95+
(arg) => arg instanceof SessionMetadata,
96+
);
9897
if (sessionMetadataIndex > -1) {
99-
const [sessionMetadata] = args.splice(sessionMetadataIndex, 1) as SessionMetadata[];
98+
const [sessionMetadata] = args.splice(
99+
sessionMetadataIndex,
100+
1,
101+
) as SessionMetadata[];
100102
return {
101103
sessionMetadata,
102104
};
@@ -110,20 +112,21 @@ export class AppLogger implements LoggerService {
110112
message: string | LogObject,
111113
optionalParams: ErrorOrMeta[] = [],
112114
) {
115+
const optionalParamsCopy = cloneDeep(optionalParams);
113116
const messageObj: LogObject = (
114117
typeof message === 'object' ? message : { message }
115118
) as LogObject;
116119

117-
const context = AppLogger.getContext(optionalParams);
118-
const error = AppLogger.getError(optionalParams);
119-
const userMetadata = AppLogger.getUserMetadata(optionalParams);
120+
const context = AppLogger.getContext(optionalParamsCopy);
121+
const error = AppLogger.getError(optionalParamsCopy);
122+
const userMetadata = AppLogger.getUserMetadata(optionalParamsCopy);
120123

121124
return {
122125
...messageObj,
123126
context,
124127
error,
125128
...userMetadata,
126-
data: optionalParams?.length ? optionalParams : undefined,
129+
data: optionalParamsCopy?.length ? optionalParamsCopy : undefined,
127130
};
128131
}
129132

0 commit comments

Comments
 (0)