Repository navigation
feat(logger): customizing logger output - #140
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.
made a few comments ;)
Additional thoughts:
- I'm not 100% sure
format_keysis 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_rateand 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
| 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.
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.
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.
@heitorlessa should I use @staticmethod then ?
There was a problem hiding this comment.
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.
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.
| # 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,timestampso by default messages in Cloudwatch logs are easier to read:Also allow for reordering override via
format_keysYou can also suppress
timestamporlocationfields by passing them in asNoneFinally you can already customize the format for
locationandtimestamp. So putting it all together:Checklist