Skip to content

Commit 5ac7712

Browse files
authored
refactor(logger): mark private members as readonly and fix code quality issues (#4350)
1 parent 613a9ae commit 5ac7712

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

packages/logger/src/Logger.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Logger extends Utility implements LoggerInterface {
117117
/**
118118
* Custom config service instance used to configure the logger.
119119
*/
120-
private customConfigService?: ConfigServiceInterface;
120+
private readonly customConfigService?: ConfigServiceInterface;
121121
/**
122122
* Whether to print the Lambda invocation event in the logs.
123123
*/
@@ -149,7 +149,7 @@ class Logger extends Utility implements LoggerInterface {
149149
/**
150150
* Standard attributes managed by Powertools that will be logged in all log items.
151151
*/
152-
private powertoolsLogData: PowertoolsLogData = <PowertoolsLogData>{
152+
private readonly powertoolsLogData: PowertoolsLogData = <PowertoolsLogData>{
153153
sampleRateValue: 0,
154154
};
155155
/**
@@ -169,14 +169,14 @@ class Logger extends Utility implements LoggerInterface {
169169
/**
170170
* Flag used to determine if the logger is initialized.
171171
*/
172-
#isInitialized = false;
172+
readonly #isInitialized: boolean = false;
173173
/**
174174
* Map used to hold the list of keys and their type.
175175
*
176176
* Because keys of different types can be overwritten, we keep a list of keys that were added and their last
177177
* type. We then use this map at log preparation time to pick the last one.
178178
*/
179-
#keys: Map<string, 'temp' | 'persistent'> = new Map();
179+
readonly #keys: Map<string, 'temp' | 'persistent'> = new Map();
180180
/**
181181
* This is the initial log leval as set during the initialization of the logger.
182182
*
@@ -263,7 +263,9 @@ class Logger extends Utility implements LoggerInterface {
263263
public constructor(options: ConstructorOptions = {}) {
264264
super();
265265
const { customConfigService, ...rest } = options;
266-
this.setCustomConfigService(customConfigService);
266+
this.customConfigService = customConfigService
267+
? customConfigService
268+
: undefined;
267269
// all logs are buffered until the logger is initialized
268270
this.setOptions(rest);
269271
this.#isInitialized = true;
@@ -1135,20 +1137,6 @@ class Logger extends Utility implements LoggerInterface {
11351137
};
11361138
}
11371139

1138-
/**
1139-
* Set the Logger's customer config service instance, which will be used
1140-
* to fetch environment variables.
1141-
*
1142-
* @param customConfigService - The custom config service
1143-
*/
1144-
private setCustomConfigService(
1145-
customConfigService?: ConfigServiceInterface
1146-
): void {
1147-
this.customConfigService = customConfigService
1148-
? customConfigService
1149-
: undefined;
1150-
}
1151-
11521140
/**
11531141
* Set the initial Logger log level based on the following order:
11541142
* 1. If a log level is set using AWS Lambda Advanced Logging Controls, it sets it.
@@ -1199,8 +1187,6 @@ class Logger extends Utility implements LoggerInterface {
11991187
if (this.isValidLogLevel(logLevelValue)) {
12001188
this.logLevel = LogLevelThreshold[logLevelValue];
12011189
this.#initialLogLevel = this.logLevel;
1202-
1203-
return;
12041190
}
12051191
}
12061192

packages/logger/src/formatter/LogFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ abstract class LogFormatter {
183183
*
184184
* @param timezone - IANA time zone identifier (e.g., "Asia/Dhaka").
185185
*/
186-
#getDateFormatter = (timezone: string): Intl.DateTimeFormat => {
186+
readonly #getDateFormatter = (timezone: string): Intl.DateTimeFormat => {
187187
const twoDigitFormatOption = '2-digit';
188188
const validTimeZone = Intl.supportedValuesOf('timeZone').includes(timezone)
189189
? timezone

packages/logger/src/formatter/PowertoolsLogFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PowertoolsLogFormatter extends LogFormatter {
2424
*
2525
* This can be a set of keys or an array of keys.
2626
*/
27-
#logRecordOrder?: LogRecordOrderKeys;
27+
readonly #logRecordOrder?: LogRecordOrderKeys;
2828

2929
public constructor(options?: PowertoolsLogFormatterOptions) {
3030
super();

packages/logger/tests/e2e/basicFeatures.middy.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,10 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
269269
const logMessages = invocationLogs[i].getFunctionLogs();
270270

271271
// Check that the X-Ray Trace ID is logged on every log
272-
const traceIds: string[] = [];
273272
for (const message of logMessages) {
274273
const log = TestInvocationLogs.parseFunctionLog(message);
275274
expect(log).toHaveProperty('xray_trace_id');
276275
expect(log.xray_trace_id).toMatch(XRAY_TRACE_ID_REGEX);
277-
traceIds.push(log.xray_trace_id as string);
278276
}
279277
}
280278
});

0 commit comments

Comments
 (0)