-
Notifications
You must be signed in to change notification settings - Fork 1
[PXP-2318] [PXP-2666] Inherit from ancestor loggers #7
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
…to NOTSET (ie to inherit)
Avantol13
left a comment
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.
🙏 it'd be awesome if you could add to the README too on how to use this. e.g. Call get_logger(log_level='info') at top level and then get_logger() at child modules and it'll propogate <- of course you can expand on that but that's the important thing to note, that you MUST set the level at the top.
| if logger.handlers: | ||
| return logger | ||
| if log_level: | ||
| if log_level not in log_levels: |
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.
if you wanna be fancy this can be one line if log_level and log_level not in log_levels:
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.
Tru. So then L89 has to turn into an elif?
if log_level and log_level not in log_levels:
[raise exception]
elif log_level:
[set level]
This block just doesn't read well because the Exception part is big and then the set level part is a oneliner after a line break:
if log_level and log_level not in log_levels:
[raise exception]
[is a longish]
[block of code, vertically]
[reader has forgotten about condition]
elif log_level:
[set level]
So maybe I can do instead:
if log_level and log_level in log_levels:
[set level]
elif log_level:
[raise exception]
[blah]
[blah]
and I think that will be less backward and read better?
I confess I prefer the current "catch error if needed else continue" pattern to "if a and b (thing) elif just a (raise error)" but yeah it ends up pretty derpy just layout-wise
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.
oh shoot, I didn't think this through or actually consider what was in the block. I also generally prefer the "catch error if needed and handle, continue" pattern to reduce the nesting where possible. but I'm sometimes privvy to the third option you provided too so the actual thing that should happen is first, so I guess I'm inconsistent 🤦♂️ you can effectively ignore me, any way is fine, not a huge deal
tests/test_logging.py
Outdated
| logger = cdislogging.get_logger('one_handler', log_level='debug') | ||
| assert len(logger.handlers) == 1 | ||
|
|
||
| def test_inheritance(): |
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.
First: yay unit tests!! :D second, I think we can break this test up into smaller tests. every time you try to log something new and assert something else could be a new test. I think it'd improve the readability a bit to chop this into pieces and add some docstrings for the tests
|
Oh, and just noticed your PR description. General note, the different sections should ideally be summarized bullet points of changes (since we use these to generate release notes). you can add further detail in the top above all the sections or, in this case, I think you basically put the information you have in the PR description in the README. and then make the breaking change bullet really short and to the point, something like: |
The default log_level argument in get_logger is now NONE, which means the default level for new loggers is NOTSET. This means that child loggers whose levels were not explicitly set will inherit logging levels and handlers from ancestor loggers. This makes it easier to do things like toggle application-wide logging levels by just setting the level on the parent. Levels and handlers can still be set on child nodes by providing the log_level argument. // So now, for example, in a parent module you can do:
logger = get_logger(__name__, log_level='debug'), and in child modules dologger = get_logger(__name__), and the child loggers will just inherit from the parent. Or, set a custom level for a child logger if desired; just supplylog_levelin the child instantiation. // https://docs.python.org/3/library/logging.htmlNote: update this https://github.com/uc-cdis/authutils/releases wherever you update cdislogging
New Features
Breaking Changes
Default log_level arg in get_logger is now NONE; now you must explicitly set the log_level on the parent logger
Bug Fixes
Improvements
Dependency updates
Deployment changes