diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index b878782ec4..5c9a4711d8 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -963,7 +963,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() }); } /** @@ -998,8 +1000,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); @@ -1008,7 +1010,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 3248a3d0a7..55188ee093 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -30,6 +30,7 @@ class EnvironmentVariablesService private logLevelVariableLegacy = '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_LAMBDA_LOG_LEVEL` environment variable. @@ -132,6 +133,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'; + } } export { EnvironmentVariablesService }; 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/ConfigServiceInterface.ts b/packages/logger/src/types/ConfigServiceInterface.ts index 127d8f2469..48092552c0 100644 --- a/packages/logger/src/types/ConfigServiceInterface.ts +++ b/packages/logger/src/types/ConfigServiceInterface.ts @@ -66,13 +66,6 @@ interface ConfigServiceInterface { */ getServiceName(): string; - /** - * It returns the value of the POWERTOOLS_DEV environment variable. - * - * @returns {boolean} - */ - isDevMode(): boolean; - /** * It returns true if the string value represents a boolean true value. * 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 515ceeb03e..1d40af9e9f 100644 --- a/packages/logger/tests/unit/EnvironmentVariablesService.test.ts +++ b/packages/logger/tests/unit/EnvironmentVariablesService.test.ts @@ -193,4 +193,29 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(0.01); }); }); + + 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'); + }); + }); }); diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index c7f9001415..beee0bce47 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), }) ); });