-
Notifications
You must be signed in to change notification settings - Fork 148
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
Comments
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. |
I have opened a PR for this. |
Thank you for stepping up, @arnabrahman - assigning it to you! |
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. |
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 likeError
orBigInt
(added in #1117).Over time, we have received requests for adding custom logic for more types like
Map
orSet
(#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.To implement the feature, the implementer should start with adding a new field of type
CustomReplacerFn
to theConstructorOptions
.This value should be handled in the
Logger.setOptions()
method, which should call a new private method calledsetJsonReplacerFn()
(to be implemented).The new
Logger.setJsonReplacerFn()
should handle the option passed during initialization, or default to theLogger.getReplacer()
method, which could also be renamed togetDefaultJsonReplacerFn()
.A reference to the replacer function should be stored in a
protected
class property calledjsonReplacerFn
(to be added)) so that it can be used by theLogger.printLog()
method and passed down to child loggers in theLogger.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.
The text was updated successfully, but these errors were encountered: