Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
JohnVillalovos wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromjlvillal/logging

Conversation

JohnVillalovos
Copy link
Member

@JohnVillalovosJohnVillalovos commentedJul 30, 2022
edited
Loading

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 ingitlab/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
alogging.basicConfig() call to get log messages to display.

[1]https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library
Related:#2080

@codecov-commenter
Copy link

codecov-commenter commentedJul 30, 2022
edited by codecovbot
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.18%. Comparing base(7a8a862) to head(b476afe).
Report is 501 commits behind head on main.

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
FlagCoverage Δ
api_func_v482.53% <90.00%> (+0.02%)⬆️
cli_func_v483.01% <90.00%> (+0.02%)⬆️
unit87.66% <100.00%> (+0.01%)⬆️

Flags with carried forward coverage won't be shown.Click here to find out more.

Files with missing linesCoverage Δ
gitlab/__init__.py100.00% <100.00%> (ø)
gitlab/_logging.py100.00% <100.00%> (ø)
gitlab/client.py98.80% <100.00%> (+<0.01%)⬆️
🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JohnVillalovosJohnVillalovosforce-pushed thejlvillal/logging branch 5 times, most recently from6b68469 tocf064f9CompareAugust 6, 2022 05:16
Copy link
Member

@nejchnejch left a 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__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Maybe we can just uselogger as suggested in the linked Python docs? Just visually also seems to make sense to me calling methods on it like that:

Suggested change
LOG=logging.getLogger(__name__)
logger=logging.getLogger(__name__)

bufferoverflow reacted with thumbs up emoji
Copy link
MemberAuthor

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 theLOG. Also in the past I have usedlogger but then found myself accidentally typinglogging 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

Copy link
MemberAuthor

@JohnVillalovosJohnVillalovosAug 25, 2022
edited
Loading

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 dologger.debug("some message")

Which can cause problems. As I unfortunately know from my own experience.

Copy link
Member

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 aLogger instance that we manipulate and not a constant asLOG would imply. I guesslog would also be somewhere in between.

bufferoverflow reacted with thumbs up emoji

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 nameLOGGER? 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())
Copy link
Member

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)?

Copy link
MemberAuthor

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.

Copy link
Member

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 useget_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

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.

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?

Copy link
Member

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 :)

Copy link
Member

@nejchnejch left a 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.

Comment on lines +8 to +18
@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"
Copy link
Member

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 simplelogging.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 toimport gitlab.

Comment on lines +9 to +20
# 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/>.
Copy link
Member

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.

Copy link
MemberAuthor

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())
Copy link
Member

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 useget_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

@lmilbaum
Copy link

I think it worth adding this feature to the features list mentioned in the README

@lmilbaum
Copy link

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 Loggingfor 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 messagesoutput will not be displayed. It is up to the client application to doa `logging.basicConfig()` call to get log messages to display.[1]https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-libraryRelated:#2080
@github-actionsGitHub Actions
Copy link

This Pull Request (PR) was marked stale because it has been open 90 days with no activity. Please remove the stale label or comment on this PR. Otherwise, it will be closed in 15 days.

@github-actionsgithub-actionsbot added stale and removed stale labelsJul 9, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@lmilbaumlmilbaumlmilbaum left review comments

@nejchnejchnejch left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@JohnVillalovos@codecov-commenter@lmilbaum@nejch

[8]ページ先頭

©2009-2025 Movatter.jp