Skip to content
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

Feature request: custom function for unserializable values in Logger (JSON replacer) #1776

Closed
1 of 2 tasks
dreamorosi opened this issue Oct 31, 2023 · 4 comments · Fixed by #2739
Closed
1 of 2 tasks
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility

Comments

@dreamorosi
Copy link
Contributor

Use case

Logger emits JSON structured logs, to do so it calls JSON.stringify() under the hood. There are several types of objects that cannot natively be serialized or that when serialized using the default behavior loose information. The Logger utility implements a custom replacer function that allows it to apply custom serialization on objects like Error or BigInt (added in #1117).

Over time, we have received requests for adding custom logic for more types like Map or Set (#1649) and in the fullness of time we expect to receive more as our customers log more and more types of objects.

To avoid bloating the library in an effort to serialize all kinds of types, we should instead enable customers to provide their own custom function that will be used internally by Logger when serializing logs.

In this way, customers who have edge cases can adapt the logger to their needs and all others can continue enjoying a lightweight logger.

Solution/User Experience

The Logger utility should have a new constructor option called jsonReplacerFn that accepts a function that once set, supersedes the default replacer and gives full control over the serialization to the customer.

type CustomReplacerFn = (key: string, value: unknown) => void;

const jsonReplacerFn = (): CustomReplacerFn => ((_key, value) => {
  if (typeof value === 'bigint') {
    return value.toString();
  }

  return value;
});

const logger = new Logger({
  jsonReplacerFn
});

To implement the feature, the implementer should start with adding a new field of type CustomReplacerFn to the ConstructorOptions.

This value should be handled in the Logger.setOptions() method, which should call a new private method called setJsonReplacerFn() (to be implemented).

The new Logger.setJsonReplacerFn() should handle the option passed during initialization, or default to the Logger.getReplacer() method, which could also be renamed to getDefaultJsonReplacerFn().

A reference to the replacer function should be stored in a protected class property called jsonReplacerFn (to be added)) so that it can be used by the Logger.printLog() method and passed down to child loggers in the Logger.createChild() method.

The implementer should also create unit tests to account for the changes, and ideally add a new documentation section using the one found in the Python version of Powertools as reference (maintainers will help with this last step).

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@dreamorosi dreamorosi added logger This item relates to the Logger Utility on-hold This item is on-hold and will be revisited in the future feature-request This item refers to a feature request for an existing or new utility labels Oct 31, 2023
@dreamorosi dreamorosi added the revisit-in-3-months Blocked issues/PRs that need to be revisited label Oct 31, 2023
@dreamorosi dreamorosi moved this from Next iteration to On hold in Powertools for AWS Lambda (TypeScript) Jan 30, 2024
@dreamorosi dreamorosi added help-wanted We would really appreciate some support from community for this one and removed on-hold This item is on-hold and will be revisited in the future revisit-in-3-months Blocked issues/PRs that need to be revisited labels Mar 11, 2024
@dreamorosi dreamorosi moved this from On hold to Backlog in Powertools for AWS Lambda (TypeScript) Mar 11, 2024
@dreamorosi
Copy link
Contributor Author

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.

@arnabrahman
Copy link
Contributor

I have opened a PR for this.

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Jul 8, 2024
@dreamorosi dreamorosi moved this from Working on it to Pending review in Powertools for AWS Lambda (TypeScript) Jul 8, 2024
@dreamorosi
Copy link
Contributor Author

Thank you for stepping up, @arnabrahman - assigning it to you!

@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (TypeScript) Jul 11, 2024
Copy link
Contributor

⚠️ 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.

@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed confirmed The scope is clear, ready for implementation labels Sep 16, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Sep 16, 2024
@dreamorosi dreamorosi removed the help-wanted We would really appreciate some support from community for this one label Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility
Projects
2 participants