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

feat(logger): introduce log key reordering functionality #2736

Merged
merged 34 commits into from
Sep 9, 2024

Conversation

arnabrahman
Copy link
Contributor

@arnabrahman arnabrahman commented Jul 5, 2024

Summary

By default, we can not change the log key positions. This PR gives the user the ability to change log key positions. We can change log order positions for any keys added in runtime

Changes

  • Mostly followed the recommendations from the comment from the actual issue.
  • A new option logRecordOrder is added for reordering.
  • Change formatAttributes function for the new option.
  • Write unit tests for this.
  • Necessary documentation has been added with examples

Issue number: #1568


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Jul 5, 2024
@pull-request-size pull-request-size bot added the size/M PR between 30-99 LOC label Jul 5, 2024
@dreamorosi dreamorosi linked an issue Jul 8, 2024 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Jul 8, 2024
@dreamorosi
Copy link
Contributor

Hi @arnabrahman, thank you for opening this PR!

I'll be reviewing it this morning and will get back to you, but overall I think we're going in the right direction.

@dreamorosi
Copy link
Contributor

i believe we should type the option for standard structured keys

I was under the same impression, but looking at the docs again I think we're mistaken, since the docs mention this:

You can change the order of standard Logger keys or any keys that will be appended later at runtime

Additionally, in the example they show usage of a key (request_id) that is not part of the standard keys and that is added at runtime.

With this in mind I think we should follow the same pattern, if possible.

I haven't tested this in the code, but at first glance if I remember correctly, I think we should be able to include all keys to the new logic by working with the additionalLogAttributes (of type LogAttributes) object.

Doing this will mean we probably remove this line, which I'm not sure if it'll have any impact on the log output.

powertoolsLogItem.addAttributes(additionalLogAttributes);

If this is a viable option, we should also modify the types of the new logRecordOrder accordingly (maybe an union between the keys of UnformattedAttributes and LogAttributes?).


Let me know if I cleared your questions 😄

@dreamorosi dreamorosi added the on-hold This item is on-hold and will be revisited in the future label Jul 29, 2024
@arnabrahman
Copy link
Contributor Author

This summer felt like an eternity but good to be back.

I was looking at your suggestions @dreamorosi, on a high level it should be possible to include all the additional keys although I have to look a bit more to validate this. But first I have to merge the main branch but I cannot. I am getting pre-commit error for markdownlint-cli2 in packages/tracer/README.md. I ran setup-local & tried again but still got the same result. Is there anything that needs to be done?

Screenshot from 2024-08-25 16-19-59

@dreamorosi
Copy link
Contributor

Hi @arnabrahman, glad to hear from you again! I hope you had a great summer and some well deserved rest 😃

The errors in the screenshot are strange because the changelog file with issue should be ignored from the markdown lint (here).

I will look into it as I have a rough idea of why this is happening, but for now I think it's safe to run the commit with the --no-verify flag, so you can skip the local checks for this one time. Either way they'll run again here in the PR. Sorry for the confusion!

@arnabrahman
Copy link
Contributor Author

arnabrahman commented Sep 2, 2024

@dreamorosi So i have progressed well on this. But I have another question. Right now the logRecordOrder passes down to child logger. What if we want to override logRecordOrder to child instance?

Currently while creating child, logFormatter is passed to child instance. https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/logger/src/Logger.ts#L308

And logFormatter is not reinitialized if it's already set https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/logger/src/Logger.ts#L1085

In this case, if we provide logRecordOrder to child, it will not be overridden. Correct me if I am wrong

@arnabrahman arnabrahman marked this pull request as ready for review September 3, 2024 03:24
@arnabrahman arnabrahman requested review from a team as code owners September 3, 2024 03:24
@dreamorosi
Copy link
Contributor

Hi @arnabrahman, thank you for the PR - I've started reviewing it and should be able to finish by tomorrow morning at the latest.

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @arnabrahman, thank you so much for the patience while I reviewed this and also for the great work!

I think we are getting very closer, I've left a few comments but the main implementation is done I think.

Let me know if you have any questions or disagree with the comments.

packages/logger/tests/unit/formatters.test.ts Outdated Show resolved Hide resolved
packages/logger/tests/unit/initializeLogger.test.ts Outdated Show resolved Hide resolved
packages/logger/src/Logger.ts Outdated Show resolved Hide resolved
docs/core/logger.md Show resolved Hide resolved
packages/logger/src/formatter/LogFormatter.ts Outdated Show resolved Hide resolved
@arnabrahman arnabrahman marked this pull request as draft September 7, 2024 04:47
@arnabrahman arnabrahman marked this pull request as ready for review September 8, 2024 10:54
@dreamorosi dreamorosi removed the on-hold This item is on-hold and will be revisited in the future label Sep 9, 2024
Copy link

sonarcloud bot commented Sep 9, 2024

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @arnabrahman for another great addition and contribution.

I really appreciate you coming back to this and working on the various iterations until we got here.

Thank you again!

PS: I'll reply to your comment on the other issue later today 😉

@dreamorosi dreamorosi merged commit 9677258 into aws-powertools:main Sep 9, 2024
11 checks passed
@arnabrahman arnabrahman deleted the 1568-log-message-ordering branch September 9, 2024 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature PRs that introduce new features or minor changes logger This item relates to the Logger Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Change ordering of default log formatter
2 participants