powertools-lambda-typescript
powertools-lambda-typescript copied to clipboard
Feature request: time zone aware timestamp in Logger
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
- [X] This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- [ ] Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
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.
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 You can assign me on this.
Thank you for picking this up @arnabrahman!
⚠️ 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.