switch the bot runtime from print to logging

Closes #14.

The runtime modules now log through the stdlib logging module. A new
log_utils.setup_logging is called once from main.py, and each module holds
its own logging.getLogger(__name__) so every line says which module it came
from.

The [INFO] and [WARNING] prefixes are gone, since the level field carries
that now. [OK], [SKIP] and [FAIL] stay in the message text: they are the
per-task outcome summary from complete_all_tasks rather than severities, and
folding them into the level would erase the run summary. They map to info,
warning and error, which is the one thing print could not express, a real
failure now sorts above a task the current UI variant simply does not ship.

Two things fall out of having levels at all:

- REWARDS_FARMER_LOG_LEVEL=DEBUG attaches the traceback to every [FAIL],
  which is the stack trace that bug reports keep having to be asked for.
- REWARDS_FARMER_LOG_FILE writes the same output to a file, so an unattended
  run can be read after the fact.

Both are off by default, so a normal run looks the same as before apart from
the timestamp and level columns.

The [FAIL] summary keeps only the first line of the exception message. A
selenium exception carries the whole msedgedriver stacktrace inside str(),
tens of lines of it, which would turn one task into one screenful and make
the log file impossible to scan. The full detail is still there with the
traceback on debug.

The console stream is stdout rather than the StreamHandler default of stderr,
so anyone already redirecting stdout keeps getting the output there, and its
error handler is set to replace. Card descriptions are scraped from the page
and are not ASCII outside the en-US market, and the Windows console encoding
raises on them.

check_selectors.py, fitts_law.py and analyze_keypresses.py are left on print.
Their output is formatted report text, and prefixing every row of a
diagnostic table with a timestamp and a level makes it harder to read.
This commit is contained in:
Ethan Stoner
2026-08-26 11:12:20 -07:00
parent e2a06275f3
commit 5c475cab05
7 changed files with 173 additions and 17 deletions
+7 -4
View File
@@ -1,6 +1,9 @@
import logging
from selenium.common.exceptions import WebDriverException, JavascriptException
from selenium import webdriver
logger = logging.getLogger(__name__)
GHOST_TAB_URLS = (
"https://ntp.msn.com/edge/ntp?locale=en-US&title=New%20tab&fre=1&dsp=1&sp=Bing&feed_dis=always&en_widget_reg=false&prerender=1&PC=U531", # has fre
"https://ntp.msn.com/edge/ntp?locale=en-US&title=New%20tab&dsp=1&sp=Bing&feed_dis=always&en_widget_reg=false&prerender=1&PC=U531" # no fre
@@ -30,7 +33,7 @@ document.dispatchEvent(new Event('visibilitychange'));
self.driver.switch_to.window(handle)
if self.driver.current_url in GHOST_TAB_URLS:
print(f"[INFO] Found ghost tab with handle {handle} and URL {self.driver.current_url}.")
logger.info("Found ghost tab with handle %s and URL %s.", handle, self.driver.current_url)
continue
self.ensure_focus()
@@ -47,17 +50,17 @@ document.dispatchEvent(new Event('visibilitychange'));
self.driver.switch_to.window(handle)
if self.driver.current_url in GHOST_TAB_URLS:
print(f"[INFO] Found ghost tab with handle {handle} and URL {self.driver.current_url}, not closing.")
logger.info("Found ghost tab with handle %s and URL %s, not closing.", handle, self.driver.current_url)
continue
tab_url = self.driver.current_url
try:
self.driver.close()
print(f"[INFO] Closed tab with handle {handle} and URL {tab_url}.")
logger.info("Closed tab with handle %s and URL %s.", handle, tab_url)
except WebDriverException:
print(f"[WARNING] Could not close tab with handle {handle} and URL {tab_url}.")
logger.warning("Could not close tab with handle %s and URL %s.", handle, tab_url)
self.problematic_tabs.add(handle)
pass