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

Commitcf064f9

Browse files
chore: add bare-minimum logging support
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
1 parent9aecc9e commitcf064f9

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

‎gitlab/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importwarnings
2020
fromtypingimportAny
2121

22+
importgitlab._logging
2223
importgitlab.config# noqa: F401
2324
fromgitlabimportutilsas_utils
2425
fromgitlab._versionimport (# noqa: F401

‎gitlab/_logging.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU Lesser General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU Lesser General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Lesser General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
importlogging
17+
fromtypingimportList
18+
19+
__all__:List[str]= []
20+
21+
# Using the `NullHandler` means that any log messages generated will not be
22+
# output unless the client application configures logging. For example by
23+
# calling `logging.basicConfig()`
24+
_module_root_logger_name=__name__.split(".",maxsplit=1)[0]
25+
logging.getLogger(_module_root_logger_name).addHandler(logging.NullHandler())

‎gitlab/client.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Wrapper for the GitLab API."""
22

3+
importlogging
34
importos
45
importre
56
importtime
@@ -16,6 +17,8 @@
1617
importgitlab.exceptions
1718
fromgitlabimportutils
1819

20+
LOG=logging.getLogger(__name__)
21+
1922
REDIRECT_MSG= (
2023
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
2124
"your GitLab URL to the correct URL to avoid issues. The redirection was from: "
@@ -520,7 +523,6 @@ def _set_auth_info(self) -> None:
520523

521524
@staticmethod
522525
defenable_debug()->None:
523-
importlogging
524526
fromhttp.clientimportHTTPConnection# noqa
525527

526528
HTTPConnection.debuglevel=1
@@ -529,6 +531,7 @@ def enable_debug() -> None:
529531
requests_log=logging.getLogger("requests.packages.urllib3")
530532
requests_log.setLevel(logging.DEBUG)
531533
requests_log.propagate=True
534+
LOG.debug("Enabled debug mode for python-gitlab")
532535

533536
def_get_session_opts(self)->Dict[str,Any]:
534537
return {

‎tests/unit/test__logging.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
importlogging
2+
3+
importpytest
4+
5+
fromgitlabimport_logging
6+
7+
8+
@pytest.fixture
9+
defLOG():
10+
returnlogging.getLogger(_logging._module_root_logger_name)
11+
12+
13+
deftest_module_root_logger_name():
14+
assert_logging._module_root_logger_name=="gitlab"
15+
16+
17+
deftest_module_name(LOG):
18+
assertLOG.name=="gitlab"
19+
20+
21+
deftest_logger_null_handler(LOG):
22+
assertlen(LOG.handlers)==1
23+
handler=LOG.handlers[0]
24+
assertisinstance(handler,logging.NullHandler)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp