-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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: log decorator util - adding automatic logs out of the box #18620
Conversation
Codecov Report
@@ Coverage Diff @@
## master #18620 +/- ##
==========================================
- Coverage 66.30% 66.13% -0.18%
==========================================
Files 1595 1595
Lines 62632 62632
Branches 6309 6309
==========================================
- Hits 41529 41422 -107
- Misses 19453 19560 +107
Partials 1650 1650
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
7e27b15
to
576f7dd
Compare
576f7dd
to
221e6c9
Compare
43c502a
to
41b674a
Compare
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.
Lgtm
…he#18620) * feat: add logger utils * fix bad definitions of under_info and debug_enable * fix pre-commit
SUMMARY
When functions are implemented, they have a specific reason and responsible for change (SRP)
Writing logs is a different concern that is not related to the reason of the functions that logging statements were added to them.
In addition, writing logs imply code duplication (imagine writing the logs in the example)
and remember, logging is a cross-cutting concern so adding logging behavior is best used as AOP
That PR adds a log decorator ability so you can decorate a function or class and you'll get logging out of the box.
Right now as of POC I added and use it under tests.
After adding the decorator, for each public method that is called, new log records will be added.
In case the logger is configured as DEBUG, for each method call (whether is private or public) a new log will be added with the passed arguments and return value.
Example
or
Implemntation details
To understand the code I recommend reading the post decorators-and-closures-in-python
When the decorator is created, it takes the decorated class or function, extracts the logger defined inside the module containing the decorated (in case a logger was not defined, a new one will be created).
with the help of the builtin inspect library the decorator will decorate all the class functions members, then for each function, the inspect will parse the function signature and wrap the function with logs statements
Important notes
Follow up tasks: