summaryrefslogtreecommitdiff
path: root/gitlab/client.py
diff options
context:
space:
mode:
authorNejc Habjan <nejc.habjan@siemens.com>2022-05-02 00:22:20 +0200
committerNejc Habjan <nejc.habjan@siemens.com>2022-07-29 23:38:46 +0200
commit08519b78e827278dc5d66b5d5a66eaeea0a500d5 (patch)
treee7380c1272bf49dac337a77f7b3143a59e398a2e /gitlab/client.py
parent17414f787a70a0d916193ac71bccce0297c4e4e8 (diff)
downloadgitlab-feat/logging-mask-tokens.tar.gz
feat(client): mask tokens by default when loggingfeat/logging-mask-tokens
Diffstat (limited to 'gitlab/client.py')
-rw-r--r--gitlab/client.py31
1 files changed, 26 insertions, 5 deletions
diff --git a/gitlab/client.py b/gitlab/client.py
index 97ca636..9f99c4f 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -524,18 +524,39 @@ class Gitlab:
self.http_username, self.http_password
)
- @staticmethod
- def enable_debug() -> None:
+ def enable_debug(self, mask_credentials: bool = True) -> None:
import logging
- from http.client import HTTPConnection # noqa
+ from http import client
- HTTPConnection.debuglevel = 1
+ client.HTTPConnection.debuglevel = 1
logging.basicConfig()
- logging.getLogger().setLevel(logging.DEBUG)
+ logger = logging.getLogger()
+ logger.setLevel(logging.DEBUG)
+
+ httpclient_log = logging.getLogger("http.client")
+ httpclient_log.propagate = True
+ httpclient_log.setLevel(logging.DEBUG)
+
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
+ # shadow http.client prints to log()
+ # https://stackoverflow.com/a/16337639
+ def print_as_log(*args: Any) -> None:
+ httpclient_log.log(logging.DEBUG, " ".join(args))
+
+ setattr(client, "print", print_as_log)
+
+ if not mask_credentials:
+ return
+
+ token = self.private_token or self.oauth_token or self.job_token
+ handler = logging.StreamHandler()
+ handler.setFormatter(utils.MaskingFormatter(masked=token))
+ logger.handlers.clear()
+ logger.addHandler(handler)
+
def _get_session_opts(self) -> Dict[str, Any]:
return {
"headers": self.headers.copy(),