diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index f0d0e8b59d..878541b965 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -930,7 +930,9 @@ class Logger extends Utility implements LoggerInterface { * @returns {void} */ private setLogFormatter(logFormatter?: LogFormatterInterface): void { - this.logFormatter = logFormatter ?? new PowertoolsLogFormatter(); + this.logFormatter = + logFormatter ?? + new PowertoolsLogFormatter({ envVarsService: this.getEnvVarsService() }); } /** @@ -965,8 +967,8 @@ class Logger extends Utility implements LoggerInterface { environment, } = options; + // order is important, EnvVarsService() is used by other methods this.setEnvVarsService(); - // order is important, it uses EnvVarsService() this.setConsole(); this.setCustomConfigService(customConfigService); this.setInitialLogLevel(logLevel); @@ -975,7 +977,6 @@ class Logger extends Utility implements LoggerInterface { this.setInitialSampleRate(sampleRateValue); this.setLogEvent(); this.setLogIndentation(); - this.addPersistentLogAttributes(persistentLogAttributes); return this; diff --git a/packages/logger/src/config/EnvironmentVariablesService.ts b/packages/logger/src/config/EnvironmentVariablesService.ts index 9627b2c018..a91ea1316f 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -29,6 +29,7 @@ class EnvironmentVariablesService private logLevelVariable = 'LOG_LEVEL'; private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE'; private sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE'; + private tzVariable = 'TZ'; /** * It returns the value of the AWS_REGION environment variable. @@ -108,6 +109,17 @@ class EnvironmentVariablesService return value && value.length > 0 ? Number(value) : undefined; } + /** + * It returns the value of the `TZ` environment variable or `UTC` if it is not set. + * + * @returns {string} + */ + public getTimezone(): string { + const value = this.get(this.tzVariable); + + return value.length > 0 ? value : 'UTC'; + } + /** * It returns true if the POWERTOOLS_DEV environment variable is set to truthy value. * diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 478e04152f..3f723d67a4 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -1,4 +1,9 @@ -import type { LogAttributes, LogFormatterInterface } from '../types/Log.js'; +import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js'; +import type { + LogAttributes, + LogFormatterInterface, + LogFormatterOptions, +} from '../types/Log.js'; import type { UnformattedAttributes } from '../types/Logger.js'; import { LogItem } from './LogItem.js'; @@ -10,6 +15,16 @@ import { LogItem } from './LogItem.js'; * @implements {LogFormatterInterface} */ abstract class LogFormatter implements LogFormatterInterface { + /** + * EnvironmentVariablesService instance. + * If set, it allows to access environment variables. + */ + protected envVarsService?: EnvironmentVariablesService; + + public constructor(options?: LogFormatterOptions) { + this.envVarsService = options?.envVarsService; + } + /** * It formats key-value pairs of log attributes. * diff --git a/packages/logger/src/types/Log.ts b/packages/logger/src/types/Log.ts index dd15a69f72..e415ee2544 100644 --- a/packages/logger/src/types/Log.ts +++ b/packages/logger/src/types/Log.ts @@ -1,3 +1,4 @@ +import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js'; import type { LogItem } from '../formatter/LogItem.js'; import type { UnformattedAttributes } from './Logger.js'; @@ -125,6 +126,14 @@ interface LogItemInterface { setAttributes(attributes: LogAttributes): void; } +type LogFormatterOptions = { + /** + * EnvironmentVariablesService instance. + * If set, it gives the LogFormatter access to environment variables. + */ + envVarsService?: EnvironmentVariablesService; +}; + /** * @interface */ @@ -175,5 +184,6 @@ export type { LogLevel, PowertoolsLog, LogItemInterface, + LogFormatterOptions, LogFormatterInterface, }; diff --git a/packages/logger/tests/unit/EnvironmentVariablesService.test.ts b/packages/logger/tests/unit/EnvironmentVariablesService.test.ts index 8ac6924f00..cb5850b3d7 100644 --- a/packages/logger/tests/unit/EnvironmentVariablesService.test.ts +++ b/packages/logger/tests/unit/EnvironmentVariablesService.test.ts @@ -153,6 +153,31 @@ describe('Class: EnvironmentVariablesService', () => { }); }); + describe('Method: getTimezone', () => { + it('returns the value of the TZ environment variable when set', () => { + // Prepare + process.env.TZ = 'Europe/London'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getTimezone(); + + // Assess + expect(value).toEqual('Europe/London'); + }); + + it('returns the default UTC value when no TZ is set', () => { + // Prepare + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getTimezone(); + + // Assess + expect(value).toEqual('UTC'); + }); + }); + describe('Method: isDevMode', () => { test('it returns true if the environment variable POWERTOOLS_DEV is "true"', () => { // Prepare diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index fe91a5ade1..8cc185aeb7 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -222,7 +222,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: {}, + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -344,7 +344,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: {}, + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -398,7 +398,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: configService, logLevel: 12, - logFormatter: {}, + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -440,7 +440,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: {}, + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -468,7 +468,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: {}, + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); diff --git a/packages/tracer/tests/helpers/tracesUtils.ts b/packages/tracer/tests/helpers/tracesUtils.ts index b12b234175..8557e60b9b 100644 --- a/packages/tracer/tests/helpers/tracesUtils.ts +++ b/packages/tracer/tests/helpers/tracesUtils.ts @@ -314,8 +314,8 @@ export { getInvocationSubsegment, splitSegmentsByName, invokeAllTestCases, - ParsedDocument, - ParsedSegment, - ParsedTrace, - AssertAnnotationParams, + type ParsedDocument, + type ParsedSegment, + type ParsedTrace, + type AssertAnnotationParams, };