Review and adjust the logging module levels

This commit is contained in:
DevilXD
2022-12-07 14:41:47 +01:00
parent 51b2da3e42
commit 76467bad65
5 changed files with 33 additions and 24 deletions

View File

@@ -77,11 +77,14 @@ WATCH_INTERVAL = timedelta(seconds=59)
DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b"
WINDOW_TITLE = f"Twitch Drops Miner v{__version__} (by DevilXD)"
# Logging
FORMATTER = logging.Formatter(
FILE_FORMATTER = logging.Formatter(
"{asctime}.{msecs:03.0f}:\t{levelname:>7}:\t{message}",
style='{',
datefmt="%Y-%m-%d %H:%M:%S",
)
OUTPUT_FORMATTER = logging.Formatter(
"{asctime}: {levelname:>7}: {message}", style='{', datefmt="%H:%M:%S"
)
class State(Enum):

11
gui.py
View File

@@ -29,7 +29,9 @@ from translate import _
from cache import ImageCache
from exceptions import ExitRequest
from utils import resource_path, Game, _T
from constants import SELF_PATH, FORMATTER, WS_TOPICS_LIMIT, MAX_WEBSOCKETS, WINDOW_TITLE, State
from constants import (
SELF_PATH, OUTPUT_FORMATTER, WS_TOPICS_LIMIT, MAX_WEBSOCKETS, WINDOW_TITLE, State
)
if sys.platform == "win32":
from registry import RegistryKey, ValueType
@@ -1837,8 +1839,11 @@ class GUIManager:
root.minsize(width=root.winfo_reqwidth(), height=root.winfo_reqheight())
# register logging handler
self._handler = _TKOutputHandler(self)
self._handler.setFormatter(FORMATTER)
logging.getLogger("TwitchDrops").addHandler(self._handler)
self._handler.setFormatter(OUTPUT_FORMATTER)
logger = logging.getLogger("TwitchDrops")
logger.addHandler(self._handler)
if (logging_level := logger.getEffectiveLevel()) < logging.ERROR:
self.print(f"Logging level: {logging.getLevelName(logging_level)}")
# gracefully handle Windows shutdown closing the application
if sys.platform == "win32":
# NOTE: this root.update() is required for the below to work - don't remove

View File

@@ -29,7 +29,7 @@ if __name__ == "__main__":
from utils import resource_path
from version import __version__
from exceptions import CaptchaRequired
from constants import SELF_PATH, FORMATTER, LOG_PATH, WINDOW_TITLE
from constants import SELF_PATH, FILE_FORMATTER, LOG_PATH, WINDOW_TITLE
class Parser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs) -> None:
@@ -155,7 +155,7 @@ if __name__ == "__main__":
logger.setLevel(settings.logging_level)
if settings.log:
handler = logging.FileHandler(LOG_PATH)
handler.setFormatter(FORMATTER)
handler.setFormatter(FILE_FORMATTER)
logger.addHandler(handler)
logging.getLogger("TwitchDrops.gql").setLevel(settings.debug_gql)
logging.getLogger("TwitchDrops.websocket").setLevel(settings.debug_ws)

View File

@@ -268,7 +268,7 @@ class _AuthState:
await coro_unless_closed(login_form.wait_for_login_press())
async def _login(self) -> str:
logger.debug("Login flow started")
logger.info("Login flow started")
gui_print = self._twitch.gui.print
login_form: LoginForm = self._twitch.gui.login
@@ -325,13 +325,13 @@ class _AuthState:
# Error handling
if "error_code" in login_response:
error_code: int = login_response["error_code"]
logger.debug(f"Login error code: {error_code}")
logger.info(f"Login error code: {error_code}")
if error_code == 1000:
logger.debug("1000: CAPTCHA is required")
logger.info("1000: CAPTCHA is required")
use_chrome = True
break
elif error_code == 3001:
logger.debug("3001: Login failed due to incorrect username or password")
logger.info("3001: Login failed due to incorrect username or password")
gui_print(_("login", "incorrect_login_pass"))
login_form.clear(password=True)
continue
@@ -339,7 +339,7 @@ class _AuthState:
3012, # Invalid authy token
3023, # Invalid email code
):
logger.debug("3012/23: Login failed due to incorrect 2FA code")
logger.info("3012/23: Login failed due to incorrect 2FA code")
if error_code == 3023:
token_kind = "email"
gui_print(_("login", "incorrect_email_code"))
@@ -353,7 +353,7 @@ class _AuthState:
3022, # Email code needed
):
# 2FA handling
logger.debug("3011/22: 2FA token required")
logger.info("3011/22: 2FA token required")
# user didn't provide a token, so ask them for it
if error_code == 3022:
token_kind = "email"
@@ -376,17 +376,17 @@ class _AuthState:
# "error_description":"client blocked from this operation"
# }
gui_print(_("login", "error_code").format(error_code=error_code))
logger.debug(str(login_response))
logger.info(str(login_response))
use_chrome = True
break
else:
ext_msg = str(login_response)
logger.debug(ext_msg)
logger.info(ext_msg)
raise LoginException(ext_msg)
# Success handling
if "access_token" in login_response:
self.access_token = cast(str, login_response["access_token"])
logger.debug("Access token granted")
logger.info("Access token granted")
login_form.clear()
break
@@ -395,7 +395,6 @@ class _AuthState:
raise CaptchaRequired()
if hasattr(self, "access_token"):
logger.debug("Access token granted")
return self.access_token
raise MinerException("Login flow finished without setting the access token")
@@ -451,7 +450,7 @@ class _AuthState:
if not self._hasattrs("access_token", "user_id"):
# looks like we're missing something
login_form: LoginForm = self._twitch.gui.login
logger.debug("Checking login")
logger.info("Checking login")
login_form.update(_("gui", "login", "logging_in"), None)
for attempt in range(2):
cookie = jar.filter_cookies(BASE_URL)
@@ -459,7 +458,7 @@ class _AuthState:
self.access_token = await self._login()
cookie["auth-token"] = self.access_token
elif not hasattr(self, "access_token"):
logger.debug("Restoring session from cookie")
logger.info("Restoring session from cookie")
self.access_token = cookie["auth-token"].value
# validate the auth token, by obtaining user_id
async with self._twitch.request(
@@ -470,7 +469,7 @@ class _AuthState:
status = response.status
if status == 401:
# the access token we have is invalid - clear the cookie and reauth
logger.debug("Restored session is invalid")
logger.info("Restored session is invalid")
assert BASE_URL.host is not None
jar.clear_domain(BASE_URL.host)
continue
@@ -481,7 +480,7 @@ class _AuthState:
raise RuntimeError("Login verification failure")
self.user_id = int(validate_response["user_id"])
cookie["persistent"] = str(self.user_id)
logger.debug(f"Login successful, user ID: {self.user_id}")
logger.info(f"Login successful, user ID: {self.user_id}")
login_form.update(_("gui", "login", "logged_in"), self.user_id)
# update our cookie and save it
jar.update_cookies(cookie, BASE_URL)
@@ -1089,7 +1088,7 @@ class Twitch:
"""
Called by a Channel when it goes online (after pending).
"""
logger.debug(f"{channel.name} goes ONLINE")
logger.info(f"{channel.name} goes ONLINE")
if (
self.can_watch(channel) # we can watch the channel that just got ONLINE
and self.should_switch(channel) # and we should!
@@ -1110,7 +1109,7 @@ class Twitch:
self.print(_("status", "goes_offline").format(channel=channel.name))
self.change_state(State.CHANNEL_SWITCH)
else:
logger.debug(f"{channel.name} goes OFFLINE")
logger.info(f"{channel.name} goes OFFLINE")
@task_wrapper
async def process_drops(self, user_id: int, message: JsonType):

View File

@@ -256,10 +256,12 @@ class Websocket:
elif raw_message.type is WSMsgType.CLOSING:
pass # skip these
elif raw_message.type is WSMsgType.ERROR:
logger.error(f"Websocket[{self._idx}] error: {format_traceback(raw_message.data)}")
ws_logger.error(
f"Websocket[{self._idx}] error: {format_traceback(raw_message.data)}"
)
raise WebsocketClosed()
else:
logger.error(f"Websocket[{self._idx}] error: Unknown message: {raw_message}")
ws_logger.error(f"Websocket[{self._idx}] error: Unknown message: {raw_message}")
def _handle_message(self, message):
# request the assigned topic to process the response