Skip to content

fix(logger): invalid time zone environment variables leads to error #2865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ abstract class LogFormatter implements LogFormatterInterface {
*/
#getDateFormatter = (timeZone: string): Intl.DateTimeFormat => {
const twoDigitFormatOption = '2-digit';
const validTimeZone = Intl.supportedValuesOf('timeZone').includes(timeZone)
? timeZone
: 'UTC';

return new Intl.DateTimeFormat('en', {
year: 'numeric',
Expand All @@ -120,7 +123,7 @@ abstract class LogFormatter implements LogFormatterInterface {
minute: twoDigitFormatOption,
second: twoDigitFormatOption,
hour12: false,
timeZone,
timeZone: validTimeZone,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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', () => {
Expand Down