Skip to content

Commit 63b2d91

Browse files
committed
Add GraphQL.enable_debug
1 parent 09ab656 commit 63b2d91

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

gitlab/client.py

+33
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,39 @@ def _set_auth_info(self) -> None:
15401540
self.http_username, self.http_password
15411541
)
15421542

1543+
def enable_debug(self, mask_credentials: bool = True) -> None:
1544+
import logging
1545+
from http import client
1546+
1547+
client.HTTPConnection.debuglevel = 1
1548+
logging.basicConfig()
1549+
logger = logging.getLogger()
1550+
logger.setLevel(logging.DEBUG)
1551+
1552+
httpclient_log = logging.getLogger("http.client")
1553+
httpclient_log.propagate = True
1554+
httpclient_log.setLevel(logging.DEBUG)
1555+
1556+
requests_log = logging.getLogger("requests.packages.urllib3")
1557+
requests_log.setLevel(logging.DEBUG)
1558+
requests_log.propagate = True
1559+
1560+
# shadow http.client prints to log()
1561+
# https://stackoverflow.com/a/16337639
1562+
def print_as_log(*args: Any) -> None:
1563+
httpclient_log.log(logging.DEBUG, " ".join(args))
1564+
1565+
setattr(client, "print", print_as_log)
1566+
1567+
if not mask_credentials:
1568+
return
1569+
1570+
token = self.private_token or self.oauth_token or self.job_token
1571+
handler = logging.StreamHandler()
1572+
handler.setFormatter(utils.MaskingFormatter(masked=token))
1573+
logger.handlers.clear()
1574+
logger.addHandler(handler)
1575+
15431576

15441577
class AsyncGraphQL(_BaseGraphQL):
15451578
def __init__(

0 commit comments

Comments
 (0)