Skip to content

Commit 89380fc

Browse files
chore: add bare-minimum logging support
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
1 parent 98bdb98 commit 89380fc

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎gitlab/__init__.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
"""Wrapper for the GitLab API."""
1818

19+
import logging
1920
import warnings
2021
from typing import Any
2122

@@ -65,3 +66,11 @@ def __getattr__(name: str) -> Any:
6566
]
6667
__all__.extend(gitlab.const._DEPRECATED)
6768
__all__.extend(gitlab.exceptions.__all__)
69+
70+
# If at some point we have "children" modules which start logging before we get
71+
# here then this should be moved before we import any of the `gitlab` modules
72+
# above.
73+
# Using the `NullHandler` means that any log messages generated will not be
74+
# output unless the client application configures logging. For example by
75+
# calling `logging.basicConfig()`
76+
logging.getLogger(__name__).addHandler(logging.NullHandler())

‎gitlab/client.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
"""Wrapper for the GitLab API."""
1818

19+
import logging
1920
import os
2021
import time
2122
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
@@ -30,6 +31,8 @@
3031
import gitlab.exceptions
3132
from gitlab import utils
3233

34+
LOG = logging.getLogger(__name__)
35+
3336
REDIRECT_MSG = (
3437
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
3538
"your GitLab URL to the correct URL to avoid issues. The redirection was from: "
@@ -526,7 +529,6 @@ def _set_auth_info(self) -> None:
526529

527530
@staticmethod
528531
def enable_debug() -> None:
529-
import logging
530532
from http.client import HTTPConnection # noqa
531533

532534
HTTPConnection.debuglevel = 1
@@ -535,6 +537,7 @@ def enable_debug() -> None:
535537
requests_log = logging.getLogger("requests.packages.urllib3")
536538
requests_log.setLevel(logging.DEBUG)
537539
requests_log.propagate = True
540+
LOG.debug("Enabled debug mode for python-gitlab")
538541

539542
def _get_session_opts(self) -> Dict[str, Any]:
540543
return {

0 commit comments

Comments
 (0)