Use logging module instead of print

This commit is contained in:
2025-06-14 17:05:59 -04:00
parent 51f987c814
commit 38821b682e
2 changed files with 22 additions and 16 deletions

View File

@@ -2,8 +2,8 @@
Type=Application
Name=ActivityWatch Amazing Marvin Watcher
Comment=Tracks Amazing Marvin tasks in ActivityWatch
Exec=poetry run awam
Path=/home/felixm/dev/awam
Exec=poetry run aw-watcher-marvin
Path=/home/felixm/dev/aw-watcher-marvin
Icon=activitywatch
StartupNotify=false
NoDisplay=true

View File

@@ -1,3 +1,4 @@
import logging
import os
import sys
from datetime import datetime, timezone
@@ -22,11 +23,11 @@ def get_tracked_task(api_token: str) -> Optional[Dict[str, Any]]:
if response.status_code == 200:
return response.json()
else:
print(f"No tracked task found (status: {response.status_code})")
logging.debug(f"No tracked task found (status: {response.status_code})")
return None
except requests.exceptions.RequestException as e:
print(f"Error fetching tracked task: {e}")
logging.error(f"Error fetching tracked task: {e}")
return None
@@ -38,10 +39,10 @@ def setup_activitywatch_client() -> Tuple[ActivityWatchClient, str]:
try:
client.create_bucket(bucket_id, event_type=event_type)
print(f"ActivityWatch bucket '{bucket_id}' ready")
logging.info(f"ActivityWatch bucket '{bucket_id}' ready")
return client, bucket_id
except Exception as e:
print(f"Error setting up ActivityWatch: {e}")
logging.error(f"Error setting up ActivityWatch: {e}")
sys.exit(1)
@@ -59,22 +60,27 @@ def create_task_event(tracked_task: Dict[str, Any]) -> Event:
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
handlers=[logging.StreamHandler()],
)
username = os.getlogin()
token = keyring.get_password("amazing-marvin-api-token", username)
if not token:
print(
"No API token found in keyring. Please store your Amazing Marvin API token:"
)
print(
logging.error(
f"No API token found in keyring. Please store your Amazing Marvin API token:"
f"Run: python -c \"import keyring; keyring.set_password('amazing-marvin-api-token', '{username}', 'YOUR_TOKEN_HERE')\""
)
sys.exit(1)
client, bucket_id = setup_activitywatch_client()
print("Starting Amazing Marvin task tracking...")
print("Press Ctrl+C to stop")
logging.info("Starting Amazing Marvin task tracking...")
logging.info("Press Ctrl+C to stop")
current_task_id = None
heartbeat_interval = 60
@@ -90,7 +96,7 @@ def main() -> None:
if task_id != current_task_id:
current_task_id = task_id
print(f"Now tracking: {task_title}")
logging.info(f"Now tracking: {task_title}")
event = create_task_event(tracked_task)
client.heartbeat(
@@ -102,12 +108,12 @@ def main() -> None:
else:
if current_task_id is not None:
current_task_id = None
print("No task currently being tracked")
logging.info("No task currently being tracked")
sleep(heartbeat_interval)
except KeyboardInterrupt:
print("\nStopping Amazing Marvin task tracking...")
logging.info("\nStopping Amazing Marvin task tracking...")
sleep(1) # Give time for queued events to be sent