Skip to content

Feature request: time zone aware timestamp in Logger #1774

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

Closed
1 of 2 tasks
dreamorosi opened this issue Oct 31, 2023 · 5 comments · Fixed by #1775 or #2710
Closed
1 of 2 tasks

Feature request: time zone aware timestamp in Logger #1774

dreamorosi opened this issue Oct 31, 2023 · 5 comments · Fixed by #1775 or #2710
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility help-wanted We would really appreciate some support from community for this one logger This item relates to the Logger Utility

Comments

@dreamorosi
Copy link
Contributor

dreamorosi commented Oct 31, 2023

Use case

By default Logger emits logs with timestamps formatted using the UTC timezone. This is the default behavior in Lambda since the TZ value is always set to UTC in the execution environment (source).

As a customer however I might want to emit logs that contain timestamps that are formatted according to a different timezone (i.e. 2021-12-12T21:21:08.921+01:00 instead of 2021-12-12T21:21:08.921Z).

The Logger utility should be able to take in account the value of the TZ environment variable, and if it's different than UTC, format the timestamps in the logs accordingly.

Solution/User Experience

By default the Lambda environment has the TZ variable set to UTC (source), if this is the case logs would be emitted as UTC like they are already. If instead the value of the TZ env variable is something else, we format the timestamp taking into account the time zone.

When in UTC, timestamps are formatted like they are today: 2021-12-12T21:21:08.921Z (source).

When using a time zone, timestamps should be formatted including the timezone offset: 2021-12-12T21:21:08.921+01:00. The offset can be positive (+) or negative (-) and it's followed by HH:MM - see ISO_8601 spec.

Below an example implementation that could be used as basis for the implementation:

const date = new Date(); // Get the current date and time

const dateFormatter = new Intl.DateTimeFormat("en", {
  year: "numeric",
  month: "2-digit",
  day: "2-digit",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false,
  timeZone: "Europe/Madrid",
});

const parts = dateFormatter.formatToParts(date);

console.log(parts);

const datePart = `${parts[4].value}-${parts[2].value}-${parts[0].value}T${parts[6].value}:${parts[8].value}:${parts[10].value}`;

const offset = -date.getTimezoneOffset();
const offsetSign = offset >= 0 ? "+" : "-";
const offsetHours = Math.abs(Math.floor(offset / 60))
  .toString()
  .padStart(2, "0");
const offsetMinutes = Math.abs(offset % 60)
  .toString()
  .padStart(2, "0");

const millisecondPart = date.getMilliseconds().toString().padStart(3, "0");
const offsetPart = `${offsetSign}${offsetHours}:${offsetMinutes}`;

const formattedDate = `${datePart}.${millisecondPart}${offsetPart}`;

console.log(formattedDate); // Format the date

Note that the code above is provided only as an example, the implementer should review it and make sure it's correct as well as applying any optimization as needed.

In terms of implementation, the implementer should modify the LogFormatter.formatTimestamp() method here so that it uses the envVarsService (if present) to fetch the value of the TZ environment variable. Then, based on the value of the variable, the implementation should be modified to either use the existing formatting, or the new time zone aware one.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@dreamorosi dreamorosi added logger This item relates to the Logger Utility on-hold This item is on-hold and will be revisited in the future feature-request This item refers to a feature request for an existing or new utility confirmed The scope is clear, ready for implementation labels Oct 31, 2023
@dreamorosi dreamorosi removed the confirmed The scope is clear, ready for implementation label Oct 31, 2023
@dreamorosi dreamorosi linked a pull request Oct 31, 2023 that will close this issue
9 tasks
@dreamorosi
Copy link
Contributor Author

Note that the issue is marked as status/on-hold. This means that at the moment we have decided to not prioritize its implementation and will likely do so after releasing promoting v2 as stable.

@dreamorosi dreamorosi added the revisit-in-3-months Blocked issues/PRs that need to be revisited label Oct 31, 2023
@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed on-hold This item is on-hold and will be revisited in the future labels Nov 3, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation on-hold This item is on-hold and will be revisited in the future and removed pending-release This item has been merged and will be released soon confirmed The scope is clear, ready for implementation labels Nov 3, 2023
@dreamorosi dreamorosi moved this from Next iteration to On hold in Powertools for AWS Lambda (TypeScript) Jan 30, 2024
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed on-hold This item is on-hold and will be revisited in the future revisit-in-3-months Blocked issues/PRs that need to be revisited labels Mar 11, 2024
@dreamorosi dreamorosi moved this from On hold to Backlog in Powertools for AWS Lambda (TypeScript) Mar 11, 2024
@dreamorosi
Copy link
Contributor Author

With v2 released, the issue is back on the backlog and ready to pick up.

If anyone from the community is interested in contributing please leave a comment below and feel free to ask any question or clarification.

@dreamorosi dreamorosi added the help-wanted We would really appreciate some support from community for this one label Mar 11, 2024
@arnabrahman
Copy link
Contributor

@dreamorosi You can assign me on this.

@dreamorosi
Copy link
Contributor Author

Thank you for picking this up @arnabrahman!

@dreamorosi dreamorosi linked a pull request Jun 30, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jul 3, 2024
Copy link
Contributor

github-actions bot commented Jul 3, 2024

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed confirmed The scope is clear, ready for implementation labels Sep 16, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility help-wanted We would really appreciate some support from community for this one logger This item relates to the Logger Utility
Projects
2 participants