Skip to content

Commit

Permalink
Rework the api_client logger (#83)
Browse files Browse the repository at this point in the history
* fix Pydactl overriding all other python loggers' logging levels

* Update api_client.py

* Update api_client.py

* Update api_client.py

---------

Co-authored-by: Seaswimmer <[email protected]>
  • Loading branch information
cswimr and cswimr authored Oct 31, 2024
1 parent 6f7d300 commit 00eb9b4
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions pydactyl/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ def http_adapter(backoff_factor, retries, extra_retry_codes):
return adapter


def set_logger(debug):
"""Configure debug logging if requested."""
if debug:
level = logging.DEBUG
else:
level = logging.ERROR

logging.basicConfig()
logging.getLogger().setLevel(level)
def get_logger() -> logging.Logger:
"""Get the default logger."""
logger = logging.getLogger(__name__)
logger.setLevel(level)
logger.propagate = True
return logger


class PterodactylClient(object):
Expand All @@ -42,7 +34,7 @@ class PterodactylClient(object):
"""

def __init__(self, url=None, api_key=None, backoff_factor=1, retries=3,
extra_retry_codes=[], debug=False):
extra_retry_codes=[], logger: logging.Logger = get_logger()):
"""Initialize a Pterodactyl class instance.
Args:
Expand All @@ -52,7 +44,7 @@ def __init__(self, url=None, api_key=None, backoff_factor=1, retries=3,
retries(int): maximum number of retries per call
extra_retry_codes(iter): list of additional integer HTTP status
codes to retry on, e.g. [502, 504]
debug(bool): enable debug logging for requests
logger(logging.Logger): the logger that Pydactyl will use
"""
if not url:
raise ClientConfigError(
Expand All @@ -64,6 +56,7 @@ def __init__(self, url=None, api_key=None, backoff_factor=1, retries=3,

self._api_key = api_key
self._url = url
self._logger = logger

self._session = requests.Session()
adapter = http_adapter(backoff_factor=backoff_factor,
Expand All @@ -72,8 +65,6 @@ def __init__(self, url=None, api_key=None, backoff_factor=1, retries=3,
self._session.mount('https://', adapter)
self._session.mount('http://', adapter)

set_logger(debug)

self._client = None
self._locations = None
self._nests = None
Expand Down

0 comments on commit 00eb9b4

Please sign in to comment.