Description
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
- 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status