Skip to content

Feature Request: support logging Set & Map types #1649

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
hfahlbusch opened this issue Aug 12, 2023 · 9 comments
Closed

Feature Request: support logging Set & Map types #1649

hfahlbusch opened this issue Aug 12, 2023 · 9 comments
Assignees
Labels
feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility rejected This is something we will not be working on. At least, not in the measurable future

Comments

@hfahlbusch
Copy link

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

  1. Create new node project
  2. Install powertools logger in node modules
  3. Copy code snippet from above
  4. 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": {}
}
@hfahlbusch hfahlbusch added triage This item has not been triaged by a maintainer, please wait bug Something isn't working labels Aug 12, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented 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 dreamorosi added logger This item relates to the Logger Utility feature-request This item refers to a feature request for an existing or new utility need-customer-feedback Requires more customers feedback before making or revisiting a decision confirmed The scope is clear, ready for implementation and removed bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Aug 12, 2023
@dreamorosi
Copy link
Contributor

dreamorosi commented 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 as Map 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.

@dreamorosi dreamorosi changed the title Logger will print Sets always as "{}" Feature Request: support logging Set type Aug 12, 2023
@hfahlbusch
Copy link
Author

hfahlbusch commented 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.)

@dreamorosi dreamorosi added the help-wanted We would really appreciate some support from community for this one label Aug 17, 2023
@dreamorosi dreamorosi removed the need-customer-feedback Requires more customers feedback before making or revisiting a decision label Sep 27, 2023
@dreamorosi dreamorosi moved this from Ideas to Backlog in Powertools for AWS Lambda (TypeScript) Sep 27, 2023
@dreamorosi dreamorosi added the good-first-issue Something that is suitable for those who want to start contributing label Sep 27, 2023
@dreamorosi dreamorosi added this to the Version 2.0 milestone Sep 27, 2023
@dreamorosi dreamorosi changed the title Feature Request: support logging Set type Feature Request: support logging Set & Map types Sep 27, 2023
@dreamorosi
Copy link
Contributor

hi @hfahlbusch apologies for the delayed response.

The suggestion to convert Set to Array and Map to Object seems sensible, I wonder however if there's any edge case that we should consider or avoid, especially with Map -> Object. From the top of my head one of the main differences between Map and plain objects is that the former can have many more types as key rather than just string like objects.

I think we can approximate and cast most of them to strings, i.e. a Map that has numbers as key is relatively safe to cast to an object that has string representations of these numbers. However for more complex types like Symbols we might not be able to do this.

I'm putting the issue on the backlog and including it in the scope for the next upcoming major version. I've also edited the type to explicitly include the Map type.

I'd like to also open the feature for other contributors. If anyone is interested in picking this up, please leave a comment below and let us know if you have any question. If everything is clear, please still leave a comment to let us know you're starting to work on this so we avoid duplicating effort, and if possible also share your comments on the implementation that you'd like to carry out.

@hfahlbusch
Copy link
Author

Hi @dreamorosi,

maybe I could implement this as a first contribution.

Do you have any additional comments to the planned serialization?
What do you think?

@dreamorosi
Copy link
Contributor

dreamorosi commented Oct 9, 2023

Hi @hfahlbusch thank you for offering your contribution, we'd love to have your contribution!

As for additional comments, the only thing to call out would be to start your branch from feat/v2 rather than from the main branch, since we'll merge the contribution into the former rather than on the v1 branch.

Other than that I would maybe recommend researching what would be the most appropriate way of coercing Set and Map types to JSON serializable values. As I mentioned before I think converting Set to an array should work for average cases but Map types might be trickier due to the fact that they can have many different types other than string as keys.

I'll assign the issue to you and mark it as work in progress, we can continue the conversation on this last point either in the issue or in the PR if you prefer.

If you have any questions on how to setup the dev environment or any other aspect of the project, please let me know - I'll be happy to support.

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Oct 9, 2023
@dreamorosi dreamorosi removed the help-wanted We would really appreciate some support from community for this one label Oct 9, 2023
@dreamorosi
Copy link
Contributor

Hi @hfahlbusch, just wanted to check in and see if there's anything that I can help with or if you have any questions 😃

@dreamorosi dreamorosi moved this from Working on it to Backlog in Powertools for AWS Lambda (TypeScript) Oct 30, 2023
@dreamorosi dreamorosi added the help-wanted We would really appreciate some support from community for this one label Oct 30, 2023
@dreamorosi dreamorosi added on-hold This item is on-hold and will be revisited in the future and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation hacktoberfest labels Oct 31, 2023
@dreamorosi dreamorosi moved this from Backlog to On hold in Powertools for AWS Lambda (TypeScript) Oct 31, 2023
@dreamorosi dreamorosi removed this from the Version 2.0 milestone Oct 31, 2023
@dreamorosi
Copy link
Contributor

Hello,

We have decided to close this feature in favor of a more powerful implementation that will allow customers to provide their own JSON replacer function. You can find more details in the dedicated issue: #1776

@dreamorosi dreamorosi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2023
@github-project-automation github-project-automation bot moved this from On hold to Coming soon in Powertools for AWS Lambda (TypeScript) Oct 31, 2023
@dreamorosi dreamorosi added rejected This is something we will not be working on. At least, not in the measurable future and removed good-first-issue Something that is suitable for those who want to start contributing on-hold This item is on-hold and will be revisited in the future labels Oct 31, 2023
@dreamorosi dreamorosi moved this from Coming soon to Closed in Powertools for AWS Lambda (TypeScript) Oct 31, 2023
@dreamorosi dreamorosi self-assigned this Oct 31, 2023
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility rejected This is something we will not be working on. At least, not in the measurable future
Projects
Development

No branches or pull requests

2 participants