-
Notifications
You must be signed in to change notification settings - Fork 421
feat(logger): customizing logger output #140
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
Conversation
Ensure that the initial key seq is: timestamp,level,location,message Closes #138
Codecov Report
@@ Coverage Diff @@
## develop #140 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 706 705 -1
Branches 67 68 +1
=========================================
- Hits 706 705 -1
Continue to review full report at Codecov.
|
@heitorlessa maybe we can make the order configurable or get some feedback on what it should be. |
Changes: * add `format_key` to allow for a custom order * remove unessarly verbose `json_formatter` with just `str` * update tests
Some keys like `location` and `timestamp` can be suppressed by setting them to None. Changes: * logger.py - Convert some methods to static functions * test_aws_lambda_logging - Add more tests and some which can be used for documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made a few comments ;)
Additional thoughts:
- I'm not 100% sure
format_keys
is descriptive enough for whoever unfamiliar with powertools to read and understand what's going on; I'll think through alternatives e.g.sort_keys
,keys_order
,log_record_order
- You had a comment about suppressing
sampling_rate
and possibly others in the future, has that changed? This doesn't address that- In hindsight (6:22am), ignore this. I think we should continue to be opinionated about what can be changed or else this will become harder to maintain
@@ -292,6 +273,29 @@ def structure_logs(self, append: bool = False, **kwargs): | |||
handler.setFormatter(JsonFormatter(**self._default_log_keys, **kwargs)) | |||
|
|||
|
|||
def _get_log_level(level: Union[str, int]) -> Union[str, int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz make this a classmethod, not a separate function - it's still part of the encapsulated logger class logic
return log_level | ||
|
||
|
||
def _get_caller_filename(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz make this a classmethod, not a separate function - it's still part of the encapsulated logger class logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heitorlessa should I use @staticmethod
then ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, yes, that's what I meant - staticmethod
.
"level": "%(levelname)s", | ||
"location": "%(funcName)s:%(lineno)d", | ||
} | ||
self.format_dict = dict.fromkeys(kwargs.pop("format_keys", ["level", "location", "message", "timestamp"])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please add a comment about insertion order here?
Maybe ordered_message_keys? Kind of unsure of the name. :)
For |
What about I'd suggest keeping as is in terms of suppression, there could be side effects on these two:
|
I will keep the log suppression as it then. We should make have a better overview of Powertools in the main website which highlights this. I still don't quite understand log sampling :-) |
Changes: * Add missing comments and docs * formatter - Group the kwargs code * logger - convert back to class static methods
|
||
|
||
def test_log_dict_key_custom_seq(stdout): | ||
# GIVEN a logger configuration with format_keys set to ["message"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# GIVEN a logger configuration with format_keys set to ["message"] | |
# GIVEN a logger configuration with log_record_order set to ["message"] |
Thank you @michaelbrewer - I've just noticed I don't have push permissions to your branch to update a tiny typo in the test comments, so I'm merging as-is and will fix-later. |
Oops. Thanks. |
Issue #, if available: #138 and #143
Description of changes:
Ensure that the initial key seq is:
level,location,message,timestamp
so by default messages in Cloudwatch logs are easier to read:Also allow for reordering override via
format_keys
You can also suppress
timestamp
orlocation
fields by passing them in asNone
Finally you can already customize the format for
location
andtimestamp
. So putting it all together:Checklist