utils: improve logging

This commit is contained in:
dece 2022-11-29 12:59:05 +01:00
parent c8c54f2e37
commit 086ec48823
2 changed files with 13 additions and 7 deletions

View file

@ -126,7 +126,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
except KeyboardInterrupt: except KeyboardInterrupt:
self.log_i("Caught keyboard interrupt.") self.log_i("Caught keyboard interrupt.")
except Exception as exc: except Exception as exc:
self.log_c(f"Caught unhandled {type(exc)}: {exc}") self.log_c(f"Caught unhandled {type(exc).__name__}: {exc}")
_, _, exc_traceback = sys.exc_info() _, _, exc_traceback = sys.exc_info()
for line in traceback.format_tb(exc_traceback): for line in traceback.format_tb(exc_traceback):
self.log_d(line.rstrip()) self.log_d(line.rstrip())

View file

@ -1,14 +1,20 @@
import random import random
from typing import Optional from typing import Callable, Optional
import requests import requests
def http_get(url: str) -> Optional[str]: def http_get(url: str, log_e: Optional[Callable] = None) -> Optional[str]:
"""Get the HTML as text from this URL.
log_e is an optional error logging function."""
try:
response = requests.get(url) response = requests.get(url)
if response.status_code == 200: response.raise_for_status()
return response.text except OSError as exc:
if log_e:
log_e(f"http_get error: {exc}")
return None return None
return response.text
def proc(proba_percentage: int) -> bool: def proc(proba_percentage: int) -> bool: