-
Notifications
You must be signed in to change notification settings - Fork 308
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
Restore missing __main__
logs
#896
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Reconfgure root logger to show all log messages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@sigmavirus24 @jaraco I think this configuration means that any logs from dependencies will come through. By default, that would be
WARNING
and above;--verbose
would enableINFO
. I'm not sure if that's desired behavior or not; it could be useful, or it could be noisy/confusing.Another option that maintains the current behavior of only enabling
twine
logs is to explicitly use thetwine
logger in__main__
, which I started with, but reverted: 698f940There 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.
Huh, that actually looks a bit nicer (hardcoding the logger's name to be
"twine"
). Whether you put that back or not, I've learned from this to always use a hardcoded logger name instead of__name__
if there's a chance of the file in question being executed as__main__
...This is one of those cases where Python's
__name__ == '__main__'
weirdness shows itself for the hack it is. 😛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.
@bayersglassey-zesty I think it's okay to use
__name__
in that situation, under most circumstances. The reason it was an issue here is because I disabled all the other existing loggers when setting up thetwine
logger.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.
That's fair.
My thinking is, I don't think there's ever a case where I want to target the logger for
__main__
.Like, it would be weird to do
logging.getLogger('__main__').setLevel(logging.INFO)
, because then if one of the functions in__main__
were ever factored out, it would suddenly be using a different logger.You know?
Anyway. Weird silly edge cases. But something tells me it's nicer to know for sure what your logger's name is. 🤷
That said, I have absolutely no preference in this case, and actually I guess I've also learned that maybe it's nicer to run things by their "entry points" (as defined by pip and installed into virtualenv's
bin
directory), since then none of the library's modules are being loaded as__main__
and possibly causing__name__
weirdness...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.
Actually, I just looked at
which twine
and clued into the fact that it's not eventwine/__init__.py
which ends up being run, it'stwine/__main__.py
.And if I make my own Python module with
__init__.py
and__main__.py
in it, it's the__main__.py
which gets run when I run the module withpython -m
.I learned something today...
And here are the docs for that: https://docs.python.org/3/library/__main__.html#main-py-in-python-packages
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.
...so actually, I guess what I'll be taking away from this is... to keep
__main__.py
as short as possible, something like:...so that all the code -- in particular, any code which creates loggers or uses
__name__
for anything other than the== '__main__'
check -- lives in a separate module. 🤔Edit: the docs actually say
__main__.py
shouldn't even bother with anif __name__ == '__main__'
check, so I updated my code snippet! https://docs.python.org/3/library/__main__.html#id1There 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.
@bayersglassey-zesty I'm glad you learned something from this. 😉
That's a good point. Most of Twine's CLI logic is in
cli.dispatch
, but this PR shows that there's some non-trivial setup that could be moved to something likecli.main
. I might follow-up with that refactoring.I think this depends on the tool and the situation. One nice thing about using
python -m package
is that you know that you're using thepackage
that's installed in thepython
environment that you're using. When you run thepackage
entrypoint, it'll be whichever one is found first on your$PATH
, which might not be the same aspython
. This is particularly relevant forpip
, and why it's a good habit to always usepython -m pip
.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.
I suspect it would be undesirable to include output from dependencies if the context (logger name) isn't present. On the other hand, since twine is using logging as the mechanism for output, maybe that's what one would expect. I'm fine either way. Let's try it out and see how it behaves.