-
Notifications
You must be signed in to change notification settings - Fork 150
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: Logger - ability to clear state #2337
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hi @rcoundon, thank you for opening this issue. As mentioned on Discord, I think it’d make sense to have this convenience method, so I am going to put the item in our backlog so it can be picked up. Just to manage expectations I think it’s unlikely it’ll make the cut for this week release, but if not, it’ll be included in the following one (~next week or the following one). |
That's great, thanks again for accepting it and putting it into the backlog so quickly |
I have started looking into this and I might have found a slight inconsistency or rather something that we should address as part of this work. Right now internally we treat what we call "persistent attributes" and "ephemeral attributes" the same - aka they're placed in the same object. The description of these and the
Below a simplified pseudo-code representation of the current implementation: // middleware AND decorator
const injectLambdaHandler = (logger: Logger) => {
let initialAttributes = {};
const before = () => {
initialAttributes = logger.getAttributes();
};
const after = () => {
logger.setPersistentLogAttributes(initialAttributes)
};
return {
before,
after,
}
} With the middleware or decorator above, the usage would be like this (showing only middleware but the decorator works the same): const logger = new Logger({
persistentLogAttributes: {
accountId: '01234567891012'
}
});
export const handler = middy(async (event: unknown) => {
logger.appendKeys({
event
});
logger.info('CANONICAL');
}).use(injectLambdaHandler(logger, { clearState: true })); The result of this would be a canonical log that contains:
The above is the behavior documented in the docs, which is fine. What is not included in the docs however is that this is also possible: const logger = new Logger({
persistentLogAttributes: {
accountId: '01234567891012'
}
});
export const handler = middy(async (event: unknown) => {
logger.appendKeys({
event
});
if (condition) {
logger.addPersistentLogAttributes({ foo: 'bar' });
}
logger.info('CANONICAL');
}).use(injectLambdaHandler(logger, { clearState: true })); Because of the names of the methods, and especially the "persistent" word, I would expect the Based on the implementation and meaning of the names, I think this should not be the case and persistent attributes should be treated as such and thus only cleared after customers remove them explicitly (i.e. To this end, I propose to consider the current behavior as unintended effect (aka bug) and rectify the implementation so that both the new Additionally, I would also:
|
commenting later today :) |
Great discussion and a nice improvement to reduce cognitive overload to everyone! Answering in line, with only one suggestion and one caveat on deprecation method.
+1, I’d suggest deprecating now but removing in the 2nd major version (e.g., 4.x) to give ample time. Logger is widely used in nearly if not all customers.
If you allow me, I'd like to suggest a different approach:
To the best of my knowledge, customers persist keys across state in two ways only: (1) Logger Constructor for simple company standard use cases, and (2) Logger Custom Formatter for complex and dynamic company standards. This means whoever set those standards have access to the constructor and often export an instance of a Logger. Whoever uses it, can still use
+1. Keeping one option for persisting across state will also simplify the documentation and make it less ambiguous. |
Thank you for weighing in Heitor, appreciate it. Let me recap what actions need to be taken so that contributors or maintainers can pick up the issue:
I'm going to mark this issue as |
I would like to pick this one up. Thanks for the clear description of the issue. |
Great to see you again @shdq, we're here or on Discord if you need anything! |
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
Hi - we're beginning to use Powertools Logger as a canonical logger. By that I mean we append various key/values to the logger as the lambda executes and then flush them to stdout by calling a write method e.g myLogger.info('canonical');
Chatting with @am29d and @dreamorosi on Discord, it was confirmed there's no way to clear the keys that were added using
appendKeys()
. This means that those keys remain present during the next warm lambda invocation.Solution/User Experience
Providing a logger method such as
clearState()
that clears down any user appended keys would solve the problem.Alternative solutions
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: