-
Notifications
You must be signed in to change notification settings - Fork 669
chore: add bare-minimum logging support #2204
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2204 +/- ##
=======================================
Coverage 96.18% 96.18%
=======================================
Files 87 88 +1
Lines 5683 5692 +9
=======================================
+ Hits 5466 5475 +9
Misses 217 217
Flags with carried forward coverage won't be shown. Click here to find out more.
|
6b68469
to
cf064f9
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.
Thanks @JohnVillalovos, just had a few questions!
@@ -16,6 +17,8 @@ | |||
import gitlab.exceptions | |||
from gitlab import utils | |||
|
|||
LOG = logging.getLogger(__name__) |
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.
Maybe we can just use logger
as suggested in the linked Python docs? Just visually also seems to make sense to me calling methods on it like that:
LOG = logging.getLogger(__name__) | |
logger = logging.getLogger(__name__) |
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.
Since it is a global I prefer the LOG
. Also in the past I have used logger
but then found myself accidentally typing logging
and not noticing what went wrong for awhile :(
Plus it is the way OpenStack does it:
https://github.com/openstack/ironic/blob/0659485d630b8651faa633f98b1802bdf244f186/ironic/drivers/modules/ipmitool.py#L61
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.
As an example it is easy to do:
logging.debug("some message")
when you really meant to do logger.debug("some message")
Which can cause problems. As I unfortunately know from my own experience.
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.
Ok, it was just an idea as it is a Logger instance that we manipulate and not a constant as LOG
would imply. I guess log
would also be somewhere in between.
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.
What do you think of the name LOGGER
? For me it is the middle ground.
# output unless the client application configures logging. For example by | ||
# calling `logging.basicConfig()` | ||
_module_root_logger_name = __name__.split(".", maxsplit=1)[0] | ||
logging.getLogger(_module_root_logger_name).addHandler(logging.NullHandler()) |
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 mght be misremembering but thought I saw an earlier version of this PR where I liked the simplicity of just doing this in __init__.py
a bit more - especially if this is extra module is being done just for testing. requests
and a lot of others do that too just in __init__.py
. Maybe we can look at other ways of testing if needed (or we can add more testing 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.
Added a docstring to explain why doing it here.
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.
To me it just shifts the issue to imports if something happens to be imported e.g. import _clients
would go before this. So I thought there might be a simple way for this, as I don't really see it done in popular python libraries. I know some use get_logger()
helpers to lazily load loggers, maybe that's the idea here and I'm not getting it 😁
I probably don't exactly understand the risks here for our own codebase or how you stumbled across this issue, so it would just be completely subjective from my side, maybe @max-wittig or @lmilbaum would be better to take a look here :P
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.
@nejch Not sure I am following the discussion. Will take a deeper look at it in few hours.
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've done some reading to figure out what the underscore means in the file name and its impact on time those lines of code are executed. Would you mind helping me figure this out?
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 think it's to indicate a private module, and it is imported here to ensure the code is evaluated before any other logging code. But I'll let John explain this :)
cf064f9
to
1509425
Compare
1509425
to
c6eb4c2
Compare
c6eb4c2
to
990098a
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.
Just got back to this @JohnVillalovos, sorry for the roundabout for the small change. A few more questions and I don't understand fully the issue I think, maybe others have an opinion.
@pytest.fixture | ||
def LOG(): | ||
return logging.getLogger(_logging._module_root_logger_name) | ||
|
||
|
||
def test_module_root_logger_name(): | ||
assert _logging._module_root_logger_name == "gitlab" | ||
|
||
|
||
def test_module_name(LOG): | ||
assert LOG.name == "gitlab" |
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.
Just a thought, but how about just doing a simple logging.getLogger("gitlab")
in our code as that would make all the string manipulation and tests unnecessary.
If the package name changes, users would have much bigger problems than this, as they would not be able to import gitlab
.
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
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.
IIRC this PR was started before we centralized the licensing into one file only, so maybe can be removed? Or if you feel strongly about license per file maybe just an SPDX header to keep it concise and machine-readable.
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'll get rid of that 👍
# output unless the client application configures logging. For example by | ||
# calling `logging.basicConfig()` | ||
_module_root_logger_name = __name__.split(".", maxsplit=1)[0] | ||
logging.getLogger(_module_root_logger_name).addHandler(logging.NullHandler()) |
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.
To me it just shifts the issue to imports if something happens to be imported e.g. import _clients
would go before this. So I thought there might be a simple way for this, as I don't really see it done in popular python libraries. I know some use get_logger()
helpers to lazily load loggers, maybe that's the idea here and I'm not getting it 😁
I probably don't exactly understand the risks here for our own codebase or how you stumbled across this issue, so it would just be completely subjective from my side, maybe @max-wittig or @lmilbaum would be better to take a look here :P
I think it worth adding this feature to the features list mentioned in the README |
Integration tests would probably help us figure out what actually happens when the library is being used by an application. More than that, it will provide us the confidence that what happens is exactly what we expect it to do. WDYT? |
Follow the Python documentation guidelines for "Configuring Logging for a Library" [1] Which is basically adding these two lines: import logging logging.getLogger(__name__).addHandler(logging.NullHandler()) Setup a very basic usage of logging in `gitlab/client.py` By using the NullHandler it means that by default any log messages output will not be displayed. It is up to the client application to do a `logging.basicConfig()` call to get log messages to display. [1] https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library Related: #2080
Follow the Python documentation guidelines for "Configuring Logging
for a Library" [1]
Which is adding these two lines:
import logging
logging.getLogger(name).addHandler(logging.NullHandler())
Setup a very basic usage of logging in
gitlab/client.py
By using the NullHandler it means that by default any log messages
output will not be displayed. It is up to the client application to do
a
logging.basicConfig()
call to get log messages to display.[1] https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library
Related: #2080