-
Notifications
You must be signed in to change notification settings - Fork 146
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: allow custom formatter to formatAttributes
on all attributes (not only the base attributes)
#1261
Comments
Hi @ghdoergeloh, thanks for opening this issue. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Thank you for opening this feature request. One of the key value propositions of the Logger utility is to allow customers to bring their own formatter so that they can comply with their unique requirements, so I definitely see value in your proposal. However I'd like to reframe/rephrase the feature request slightly. ContextAt the moment, the logic in charge of formatting attributes resides within the private method called By default, Logger comes with an opinionated As you pointed out, any attribute present in the The method was designed as such because the formatting done in the Proposed rephrasing/refocusWith this in mind, I think that the path forward should be to consider adding to custom log formatters, aka log formatters that extend the Proposed solutions / DX
The formatAttributes(attributes: UnformattedAttributes): LogAttributes As discussed for now we are only passing some attributes to it, and excluding any Option 1To give full control to a custom log formatter, all three types of attributes should be passed to it, which would require to change the method signature to something like: formatAttributes(attributes: UnformattedAttributes, persistentLogAttributes: LogAttributes, extraInput: LogItemExtraInput): LogAttributes This would allow us to move some of the some of the logic currently in the At the same time, and more interestingly, this would give full control to users extending the This is how Powertools for Python currently behaves, however changing out library's logger now would represent a breaking change and will require us to wait for a major version release. Option 2A non-breaking way of allowing some type of formatting, but not sorting would be to extend the formatPersistentAttributes(persistentLogAttributes: LogAttributes): void
formatExtraAttributes(extraAttributes: LogAttributes): void which could then optionally overloaded by customers wanting to format one or the other group of attributes. These two methods would be called as part of the While this could be a decent stop-gap solution, I'm not convinced it's the best way forward for the long term and in line with the key value proposition of Logger as an utility - which is to provide full customization of the log format. What do you think? Would appreciate people's opinion on this + additional / alternative proposals. |
formatAttributes
on all attributes (not only the base attributes)formatAttributes
on all attributes (not only the base attributes)
I do also think that your first solution would be the best way. But if you decide for the second option, you could think about "piping" the log object through these methods and each method can use the existing log object from the earlier called method. So you would call formatAttributes, take the result an push it together with the persistent attributes to the formatPersistentAttributes method. So you would be able to format and also to sort the attributes in your message object. Same with the formatExtraAttributes. |
definitely +1 this feature - as part of a team that just refactored our custom logger to use the powertools logger, i think the expectation with a custom log formatter is that it will format all the attributes within a log item, not just the base or persistent attributes. also, a potential solution - and apologies if this is naive, still learning the codebase - couldn't you simply call formatAttributes on all the attributes, then set the attributes of the logItem to the formatted attributes? so within
as far as i can tell, this should still give complete control to the custom LogFormatter, without introducing a breaking change. let me know what you all think! i'm happy to help with the implementation of a solution regardless which direction we go 🙂 |
Hi @quintinchris, thank you for adding to this issue, and also for offering to help! The solution you propose could work, however the challenge that we are having is due to a decision that was made on our side early on to exposing to the consumer (a.k.a. developers implementing custom log formatters) only a subset of the attributes. If I'm understanding correctly the suggestion, this would mean that the signature of the This, while patching the problem in the short term, would mean asking developers to handle this unknown type while implementing their version of With that said, I want to share that I am adding the feature to our backlog as we are definitely going to implement a version of this (hopefully with help & inputs from the community). We are currently discussing supporting other observability providers other than CloudWatch. This is still a early stages discussion that is happening in the form of an RFC in our sister project Powertools for Python (aws-powertools/powertools-lambda-python#2014) and that will be ported to this version as well. The main gist of the proposal is that we are going to add support to other providers by extending the Implementing this issue specifically, will pave the way for that other feature so we will address it sooner rather than later. At the moment I don't have an ETA to share, mainly because this feature is included in the version 2 planning (since it's a breaking change), but I should be able to give one in the coming weeks. |
Hi everyone, we have started working on a RFC related this topic. If you're interested in reading the current proposal and leaving your feedback please do so at #1500. |
The PR for the RFC mentioned above has been merged several weeks ago. We are actively working on the next major release and plan on start publishing public alpha releases soon. You can follow the progress of the release here: #1714 |
|
Use case
In my use case there is a compliance that enforces the developer to put some basic attributes in the root level of a log json. All the other additional data like a error or other data to identify the reason or context of that log should be placed under a attribute "data".
Since the
formatAttributes
function of a Formatter only gets theunformattedBaseAttributes
it is not possible to sort or format the other attributes.Solution/User Experience
I would like to pass any attribute to the logs and enforce a nice ordering of the log in the formatter:
which should then result in a object like this:
Alternative solutions
My current workaround is to extend the Logger Class and do the sorting in the debug,info,warn and error method:
Acknowledgment
The text was updated successfully, but these errors were encountered: