From 827ac62cadc83abe3ac2f4cce76609f74aadce3e Mon Sep 17 00:00:00 2001 From: daschaa Date: Wed, 31 Jul 2024 12:34:21 +0200 Subject: [PATCH 1/2] fix(logger): invalid time zone environment variables leads to error --- packages/logger/src/formatter/LogFormatter.ts | 13 ++++++++++ .../formatter/PowertoolsLogFormatter.test.ts | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index f593e56f02..eab342a8bd 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -112,6 +112,19 @@ abstract class LogFormatter implements LogFormatterInterface { #getDateFormatter = (timeZone: string): Intl.DateTimeFormat => { const twoDigitFormatOption = '2-digit'; + if (!Intl.supportedValuesOf('timeZone').includes(timeZone)) { + return new Intl.DateTimeFormat('en', { + year: 'numeric', + month: twoDigitFormatOption, + day: twoDigitFormatOption, + hour: twoDigitFormatOption, + minute: twoDigitFormatOption, + second: twoDigitFormatOption, + hour12: false, + timeZone: 'UTC', + }); + } + return new Intl.DateTimeFormat('en', { year: 'numeric', month: twoDigitFormatOption, diff --git a/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts index 81fb7c7aeb..810bb2f3e0 100644 --- a/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts +++ b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts @@ -320,7 +320,7 @@ describe('Class: PowertoolsLogFormatter', () => { test('it formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset', () => { // Prepare process.env.TZ = 'America/New_York'; - /* + /* Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes. The positive value indicates that `America/New_York` is behind UTC. */ @@ -341,7 +341,7 @@ describe('Class: PowertoolsLogFormatter', () => { process.env.TZ = 'America/New_York'; const mockDate = new Date('2016-06-20T12:08:10.910Z'); jest.useFakeTimers().setSystemTime(mockDate); - /* + /* Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes. The positive value indicates that `America/New_York` is behind UTC. */ @@ -362,7 +362,7 @@ describe('Class: PowertoolsLogFormatter', () => { process.env.TZ = 'America/New_York'; const mockDate = new Date('2016-06-20T00:08:10.910Z'); jest.useFakeTimers().setSystemTime(mockDate); - /* + /* Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes. The positive value indicates that `America/New_York` is behind UTC. */ @@ -381,7 +381,7 @@ describe('Class: PowertoolsLogFormatter', () => { test('if `envVarsService` is not set, ensures timestamp is formatted to `UTC` even with `America/New_York` timezone', () => { // Prepare process.env.TZ = 'America/New_York'; - /* + /* Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes. The positive value indicates that `America/New_York` is behind UTC. */ @@ -471,6 +471,22 @@ describe('Class: PowertoolsLogFormatter', () => { // Assess expect(timestamp).toEqual('2016-06-20T12:08:10.000Z'); }); + + test('it is using UTC timezone when env var is set to :/etc/localtime', () => { + // Prepare + process.env.TZ = ':/etc/localtime'; + + jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0); + const formatter = new PowertoolsLogFormatter({ + envVarsService: new EnvironmentVariablesService(), + }); + + // Act + const timestamp = formatter.formatTimestamp(new Date()); + + // Assess + expect(timestamp).toEqual('2016-06-20T12:08:10.000+00:00'); + }); }); describe('Method: getCodeLocation', () => { From 23fdcc979babd8f689b7ed8b7483431cad6b2646 Mon Sep 17 00:00:00 2001 From: daschaa Date: Wed, 31 Jul 2024 15:12:19 +0200 Subject: [PATCH 2/2] chore(logger): make the code less verbose --- packages/logger/src/formatter/LogFormatter.ts | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index eab342a8bd..bb34b4b533 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -111,19 +111,9 @@ abstract class LogFormatter implements LogFormatterInterface { */ #getDateFormatter = (timeZone: string): Intl.DateTimeFormat => { const twoDigitFormatOption = '2-digit'; - - if (!Intl.supportedValuesOf('timeZone').includes(timeZone)) { - return new Intl.DateTimeFormat('en', { - year: 'numeric', - month: twoDigitFormatOption, - day: twoDigitFormatOption, - hour: twoDigitFormatOption, - minute: twoDigitFormatOption, - second: twoDigitFormatOption, - hour12: false, - timeZone: 'UTC', - }); - } + const validTimeZone = Intl.supportedValuesOf('timeZone').includes(timeZone) + ? timeZone + : 'UTC'; return new Intl.DateTimeFormat('en', { year: 'numeric', @@ -133,7 +123,7 @@ abstract class LogFormatter implements LogFormatterInterface { minute: twoDigitFormatOption, second: twoDigitFormatOption, hour12: false, - timeZone, + timeZone: validTimeZone, }); };