Skip to content

Commit 323f610

Browse files
committed
chore(logger): set default UTC timezone (#1775)
* chore(parameters): add export types * chore(logger): set default utc timezone * chore(logger): pass down envvarsservice to log formatter
1 parent 7f8b08f commit 323f610

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

Diff for: packages/logger/src/Logger.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,9 @@ class Logger extends Utility implements LoggerInterface {
963963
* @returns {void}
964964
*/
965965
private setLogFormatter(logFormatter?: LogFormatterInterface): void {
966-
this.logFormatter = logFormatter ?? new PowertoolsLogFormatter();
966+
this.logFormatter =
967+
logFormatter ??
968+
new PowertoolsLogFormatter({ envVarsService: this.getEnvVarsService() });
967969
}
968970

969971
/**
@@ -998,8 +1000,8 @@ class Logger extends Utility implements LoggerInterface {
9981000
environment,
9991001
} = options;
10001002

1003+
// order is important, EnvVarsService() is used by other methods
10011004
this.setEnvVarsService();
1002-
// order is important, it uses EnvVarsService()
10031005
this.setConsole();
10041006
this.setCustomConfigService(customConfigService);
10051007
this.setInitialLogLevel(logLevel);
@@ -1008,7 +1010,6 @@ class Logger extends Utility implements LoggerInterface {
10081010
this.setInitialSampleRate(sampleRateValue);
10091011
this.setLogEvent();
10101012
this.setLogIndentation();
1011-
10121013
this.addPersistentLogAttributes(persistentLogAttributes);
10131014

10141015
return this;

Diff for: packages/logger/src/config/EnvironmentVariablesService.ts

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class EnvironmentVariablesService
3030
private logLevelVariableLegacy = 'LOG_LEVEL';
3131
private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE';
3232
private sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE';
33+
private tzVariable = 'TZ';
3334

3435
/**
3536
* It returns the value of the `AWS_LAMBDA_LOG_LEVEL` environment variable.
@@ -132,6 +133,17 @@ class EnvironmentVariablesService
132133

133134
return value && value.length > 0 ? Number(value) : undefined;
134135
}
136+
137+
/**
138+
* It returns the value of the `TZ` environment variable or `UTC` if it is not set.
139+
*
140+
* @returns {string}
141+
*/
142+
public getTimezone(): string {
143+
const value = this.get(this.tzVariable);
144+
145+
return value.length > 0 ? value : 'UTC';
146+
}
135147
}
136148

137149
export { EnvironmentVariablesService };

Diff for: packages/logger/src/formatter/LogFormatter.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { LogAttributes, LogFormatterInterface } from '../types/Log.js';
1+
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
2+
import type {
3+
LogAttributes,
4+
LogFormatterInterface,
5+
LogFormatterOptions,
6+
} from '../types/Log.js';
27
import type { UnformattedAttributes } from '../types/Logger.js';
38
import { LogItem } from './LogItem.js';
49

@@ -10,6 +15,16 @@ import { LogItem } from './LogItem.js';
1015
* @implements {LogFormatterInterface}
1116
*/
1217
abstract class LogFormatter implements LogFormatterInterface {
18+
/**
19+
* EnvironmentVariablesService instance.
20+
* If set, it allows to access environment variables.
21+
*/
22+
protected envVarsService?: EnvironmentVariablesService;
23+
24+
public constructor(options?: LogFormatterOptions) {
25+
this.envVarsService = options?.envVarsService;
26+
}
27+
1328
/**
1429
* It formats key-value pairs of log attributes.
1530
*

Diff for: packages/logger/src/types/ConfigServiceInterface.ts

-7
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ interface ConfigServiceInterface {
6666
*/
6767
getServiceName(): string;
6868

69-
/**
70-
* It returns the value of the POWERTOOLS_DEV environment variable.
71-
*
72-
* @returns {boolean}
73-
*/
74-
isDevMode(): boolean;
75-
7669
/**
7770
* It returns true if the string value represents a boolean true value.
7871
*

Diff for: packages/logger/src/types/Log.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
12
import type { LogItem } from '../formatter/LogItem.js';
23
import type { UnformattedAttributes } from './Logger.js';
34

@@ -125,6 +126,14 @@ interface LogItemInterface {
125126
setAttributes(attributes: LogAttributes): void;
126127
}
127128

129+
type LogFormatterOptions = {
130+
/**
131+
* EnvironmentVariablesService instance.
132+
* If set, it gives the LogFormatter access to environment variables.
133+
*/
134+
envVarsService?: EnvironmentVariablesService;
135+
};
136+
128137
/**
129138
* @interface
130139
*/
@@ -175,5 +184,6 @@ export type {
175184
LogLevel,
176185
PowertoolsLog,
177186
LogItemInterface,
187+
LogFormatterOptions,
178188
LogFormatterInterface,
179189
};

Diff for: packages/logger/tests/unit/EnvironmentVariablesService.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,29 @@ describe('Class: EnvironmentVariablesService', () => {
193193
expect(value).toEqual(0.01);
194194
});
195195
});
196+
197+
describe('Method: getTimezone', () => {
198+
it('returns the value of the TZ environment variable when set', () => {
199+
// Prepare
200+
process.env.TZ = 'Europe/London';
201+
const service = new EnvironmentVariablesService();
202+
203+
// Act
204+
const value = service.getTimezone();
205+
206+
// Assess
207+
expect(value).toEqual('Europe/London');
208+
});
209+
210+
it('returns the default UTC value when no TZ is set', () => {
211+
// Prepare
212+
const service = new EnvironmentVariablesService();
213+
214+
// Act
215+
const value = service.getTimezone();
216+
217+
// Assess
218+
expect(value).toEqual('UTC');
219+
});
220+
});
196221
});

Diff for: packages/logger/tests/unit/Logger.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('Class: Logger', () => {
222222
envVarsService: expect.any(EnvironmentVariablesService),
223223
customConfigService: undefined,
224224
logLevel: 8,
225-
logFormatter: {},
225+
logFormatter: expect.any(PowertoolsLogFormatter),
226226
})
227227
);
228228
});
@@ -344,7 +344,7 @@ describe('Class: Logger', () => {
344344
envVarsService: expect.any(EnvironmentVariablesService),
345345
customConfigService: undefined,
346346
logLevel: 8,
347-
logFormatter: {},
347+
logFormatter: expect.any(PowertoolsLogFormatter),
348348
})
349349
);
350350
});
@@ -398,7 +398,7 @@ describe('Class: Logger', () => {
398398
envVarsService: expect.any(EnvironmentVariablesService),
399399
customConfigService: configService,
400400
logLevel: 12,
401-
logFormatter: {},
401+
logFormatter: expect.any(PowertoolsLogFormatter),
402402
})
403403
);
404404
});
@@ -440,7 +440,7 @@ describe('Class: Logger', () => {
440440
envVarsService: expect.any(EnvironmentVariablesService),
441441
customConfigService: undefined,
442442
logLevel: 8,
443-
logFormatter: {},
443+
logFormatter: expect.any(PowertoolsLogFormatter),
444444
})
445445
);
446446
});
@@ -468,7 +468,7 @@ describe('Class: Logger', () => {
468468
envVarsService: expect.any(EnvironmentVariablesService),
469469
customConfigService: undefined,
470470
logLevel: 8,
471-
logFormatter: {},
471+
logFormatter: expect.any(PowertoolsLogFormatter),
472472
})
473473
);
474474
});

0 commit comments

Comments
 (0)