Description
Expected Behaviour
If I enter a Set as a value of the LogItemExtraInput it should print it's elements like the console.log()
and not return {}.
I was thinking there were no data in the set and searched a while for the error.
Current Behaviour
Set with elements will be printed as {}
Code snippet
import { Logger } from '@aws-lambda-powertools/logger';
const log = new Logger({
logLevel: 'DEBUG'
});
const a = new Set();
a.add(1);
a.add(2);
log.debug('My set not logged properly', { data: a});
//{"level":"DEBUG","message":"My set not logged properly","service":"service_undefined","timestamp":"2023-08-12T14:27:00.205Z","data":{}}
console.log('My set logged', a);
// My set logged Set(2) { 1, 2 }
Steps to Reproduce
- Create new node project
- Install powertools logger in node modules
- Copy code snippet from above
- Run and see missing Set data
Possible Solution
I digged a little into the code.
It seems the extra input is stringified with JSON.stringify which cannot handle Sets in the expected way, because the values are not properties of the Set.
Could be added to the getReplacer()
in Logger.js that also handles bigints.
Maybe convert Set to an array [...mySet] or create string representation manually.
Powertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
18.x
Packaging format used
npm
Execution logs
Example in the logs:
{
"logLevel": "DEBUG",
"message": "Modified products and documents",
"awsRegion": "eu-central-1",
"service": "cache-invalidation",
"timestamp": "2023-08-12T13:43:30.197Z",
"correlationIds": {
"xRayTraceId": "xxxxxxxxxxxxxxxxxxxxxxxx"
},
"durationInMs": 1291,
"changedAfter": "2023-08-09T22:59:56.820Z",
"numberOfProducts": 4,
"numberOfDocuments": 5,
"productIds": [
[
"PROD1",
"sv-se"
],
[
"PROD2",
"no-no"
],
[
"PROD3",
"da-dk"
],
[
"PROD4",
"no-no"
]
],
"documentIds": {}
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Activity
boring-cyborg commentedon Aug 12, 2023
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link
dreamorosi commentedon Aug 12, 2023
Hi @hfahlbusch thank you for taking the time to open this issue.
Before emitting a log the Logger utility calls
JSON.stringify()
on the log object.Like you mentioned
Set
, as well asMap
and others complex types, is not natively JSON-serializable (docs) so the behavior is not a bug but a missing feature.I think it could be a good addition to the library so I'm going to change the issue type and mark it as feature request.
If there's interest we could definitely add support for those types after agreeing on a format to use.
[-]Logger will print Sets always as "{}"[/-][+]Feature Request: support logging `Set` type[/+]hfahlbusch commentedon Aug 12, 2023
Yeah, you are right!
I had a different mental model in my head, I was assuming it behaves more like the console.log not knowing that it's basically a JSON.stringify(). So that's why I framed it as a bug.
I guess with the advent of Sets and Maps it would be a good addition. In my opinion it brings some more comfort.
But for me even the confusion was causing more trouble, than "just transforming the Set to an array" to see it's values.
I would suggest to convert Set to Array and Map to Object, so it stays valid JSON or are you thinking about "arbitrary" strings to make them distinguishable?
(Maybe easy processing of logs as input for another tool could be also a factor here.)
33 remaining items