Skip to content

Feature Request: support logging Set & Map types #1649

Not planned
@hfahlbusch

Description

@hfahlbusch

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": {}
}

Activity

added
triageThis item has not been triaged by a maintainer, please wait
bugSomething isn't working
on Aug 12, 2023
boring-cyborg

boring-cyborg commented on Aug 12, 2023

@boring-cyborg

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

added
loggerThis item relates to the Logger Utility
feature-requestThis item refers to a feature request for an existing or new utility
need-customer-feedbackRequires more customers feedback before making or revisiting a decision
confirmedThe scope is clear, ready for implementation
and removed
bugSomething isn't working
triageThis item has not been triaged by a maintainer, please wait
on Aug 12, 2023
dreamorosi

dreamorosi commented on Aug 12, 2023

@dreamorosi
Contributor

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.

changed the title [-]Logger will print Sets always as "{}"[/-] [+]Feature Request: support logging `Set` type[/+] on Aug 12, 2023
hfahlbusch

hfahlbusch commented on Aug 12, 2023

@hfahlbusch
Author

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

added
help-wantedWe would really appreciate some support from community for this one
on Aug 17, 2023

33 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature-requestThis item refers to a feature request for an existing or new utilityloggerThis item relates to the Logger UtilityrejectedThis is something we will not be working on. At least, not in the measurable future

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @dreamorosi@hfahlbusch

      Issue actions

        Feature Request: support logging `Set` & `Map` types · Issue #1649 · aws-powertools/powertools-lambda-typescript