diff --git a/wikibaseintegrator/entities/baseentity.py b/wikibaseintegrator/entities/baseentity.py index f9229299..db073899 100644 --- a/wikibaseintegrator/entities/baseentity.py +++ b/wikibaseintegrator/entities/baseentity.py @@ -240,7 +240,7 @@ def _write(self, data: Dict = None, summary: str = None, login: _Login = None, a try: json_result: dict = mediawiki_api_call_helper(data=payload, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs) except Exception: - print('Error while writing to the Wikibase instance') + logging.error('Error while writing to the Wikibase instance') raise else: if 'error' in json_result and 'messages' in json_result['error']: diff --git a/wikibaseintegrator/wbi_backoff.py b/wikibaseintegrator/wbi_backoff.py index edbbf0f1..b6082dfa 100644 --- a/wikibaseintegrator/wbi_backoff.py +++ b/wikibaseintegrator/wbi_backoff.py @@ -1,6 +1,7 @@ """ WikibaseIntegrator implementation of backoff python library. """ +import logging import sys from functools import partial from json import JSONDecodeError @@ -14,8 +15,8 @@ def wbi_backoff_backoff_hdlr(details): exc_type, exc_value, _ = sys.exc_info() if exc_type == JSONDecodeError: - print(exc_value.doc) # pragma: no cover - print("Backing off {wait:0.1f} seconds afters {tries} tries calling function with args {args} and kwargs {kwargs}".format(**details)) # pylint: disable=consider-using-f-string + logging.error(exc_value.doc) # pragma: no cover + logging.error("Backing off {wait:0.1f} seconds afters {tries} tries calling function with args {args} and kwargs {kwargs}".format(**details)) # pylint: disable=consider-using-f-string def wbi_backoff_check_json_decode_error(e) -> bool: diff --git a/wikibaseintegrator/wbi_helpers.py b/wikibaseintegrator/wbi_helpers.py index e3922633..741a0f8c 100644 --- a/wikibaseintegrator/wbi_helpers.py +++ b/wikibaseintegrator/wbi_helpers.py @@ -67,11 +67,11 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess try: response = session.request(method=method, url=mediawiki_api_url, **kwargs) except requests.exceptions.ConnectionError as e: - print(f"Connection error: {e}. Sleeping for {retry_after} seconds.") + logging.error(f"Connection error: {e}. Sleeping for {retry_after} seconds.") sleep(retry_after) continue if response.status_code in (500, 502, 503, 504): - print(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.") + logging.error(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.") sleep(retry_after) continue @@ -87,7 +87,7 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess error_msg_names = {x.get('name') for x in json_data['error']['messages']} if 'actionthrottledtext' in error_msg_names: # pragma: no cover sleep_sec = int(response.headers.get('retry-after', retry_after)) - print(f"{datetime.datetime.utcnow()}: rate limited. sleeping for {sleep_sec} seconds") + logging.error(f"{datetime.datetime.utcnow()}: rate limited. sleeping for {sleep_sec} seconds") sleep(sleep_sec) continue @@ -100,13 +100,13 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess sleep_sec = max(sleep_sec, 5) # The number of second can't be more than retry_after sleep_sec = min(sleep_sec, retry_after) - print(f"{datetime.datetime.utcnow()}: maxlag. sleeping for {sleep_sec} seconds") + logging.error(f"{datetime.datetime.utcnow()}: maxlag. sleeping for {sleep_sec} seconds") sleep(sleep_sec) continue # readonly if 'code' in json_data['error'] and json_data['error']['code'] == 'readonly': # pragma: no cover - print(f'The Wikibase instance is currently in readonly mode, waiting for {retry_after} seconds') + logging.error(f'The Wikibase instance is currently in readonly mode, waiting for {retry_after} seconds') sleep(retry_after) continue @@ -248,17 +248,17 @@ def execute_sparql_query(query: str, prefix: str = None, endpoint: str = None, u try: response = helpers_session.post(sparql_endpoint_url, params=params, headers=headers) except requests.exceptions.ConnectionError as e: - print(f"Connection error: {e}. Sleeping for {retry_after} seconds.") + logging.error(f"Connection error: {e}. Sleeping for {retry_after} seconds.") sleep(retry_after) continue if response.status_code in (500, 502, 503, 504): - print(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.") + logging.error(f"Service unavailable (HTTP Code {response.status_code}). Sleeping for {retry_after} seconds.") sleep(retry_after) continue if response.status_code == 429: if 'retry-after' in response.headers.keys(): retry_after = int(response.headers['retry-after']) - print(f"Too Many Requests (429). Sleeping for {retry_after} seconds") + logging.error(f"Too Many Requests (429). Sleeping for {retry_after} seconds") sleep(retry_after) continue response.raise_for_status() diff --git a/wikibaseintegrator/wbi_login.py b/wikibaseintegrator/wbi_login.py index cdf847f5..c750db6f 100644 --- a/wikibaseintegrator/wbi_login.py +++ b/wikibaseintegrator/wbi_login.py @@ -239,9 +239,9 @@ def __init__(self, user: str = None, password: str = None, mediawiki_api_url: st raise LoginError(f"Login failed. Reason: '{login_result['login']['reason']}'") if 'warnings' in login_result: - print("MediaWiki login warnings messages:") + logging.warning("MediaWiki login warnings messages:") for message in login_result['warnings']: - print(f"* {message}: {login_result['warnings'][message]['*']}") + logging.warning(f"* {message}: {login_result['warnings'][message]['*']}") super().__init__(session=session, token_renew_period=token_renew_period, user_agent=user_agent, mediawiki_api_url=mediawiki_api_url) @@ -296,9 +296,9 @@ def __init__(self, user: str = None, password: str = None, mediawiki_api_url: st raise LoginError(f"Login failed ({login_result['error']['code']}). Message: '{login_result['error']['info']}'") if 'warnings' in login_result: - print("MediaWiki login warnings messages:") + logging.warning("MediaWiki login warnings messages:") for message in login_result['warnings']: - print(f"* {message}: {login_result['warnings'][message]['*']}") + logging.warning(f"* {message}: {login_result['warnings'][message]['*']}") super().__init__(session=session, token_renew_period=token_renew_period, user_agent=user_agent, mediawiki_api_url=mediawiki_api_url)