From a15b5ffda0cb953f230cf1f0c8a4c931f5fae0ea Mon Sep 17 00:00:00 2001 From: agatha Date: Fri, 10 Nov 2023 22:33:08 -0500 Subject: [PATCH] Implement basic logging --- gitmon/api.py | 11 ++++++++--- gitmon/logger.py | 8 ++++++++ main.py | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 gitmon/logger.py diff --git a/gitmon/api.py b/gitmon/api.py index b740db7..eb52918 100644 --- a/gitmon/api.py +++ b/gitmon/api.py @@ -1,10 +1,13 @@ """GitHub API module""" -import logging import requests from datetime import datetime from time import sleep from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry +from loguru import logger +from .logger import setup_logger + +setup_logger() class GitHubApi: @@ -32,14 +35,14 @@ class GitHubApi: # Check rate limits if self.rate_remaining == 0 and self.rate_reset > datetime.now(): - logging.warning(f'Out of API calls. Sleeping until {self.rate_reset}.') + logger.warning(f'Out of API calls. Sleeping until {self.rate_reset}.') delta = self.rate_reset - datetime.now() sleep(delta.total_seconds()) try: response = self.session.get(url, params=params) except requests.RequestException as e: - logging.warning(f'Failed to execute GET request: {str(e)}') + logger.warning(f'Failed to execute GET request: {str(e)}') return None # 304 Not Modified @@ -53,6 +56,8 @@ class GitHubApi: """Parse headers to update rate limits.""" self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset'])) self.rate_remaining = int(headers['x-ratelimit-remaining']) + logger.info(f'{self.rate_remaining} calls remaining until {self.rate_reset}') + def get_events(self, page=None): """Fetch GitHub events.""" diff --git a/gitmon/logger.py b/gitmon/logger.py new file mode 100644 index 0000000..95b0dfe --- /dev/null +++ b/gitmon/logger.py @@ -0,0 +1,8 @@ +"""Logging module.""" +import sys +from loguru import logger + + +def setup_logger(): + logger.remove() + logger.add(sys.stdout, level='INFO') diff --git a/main.py b/main.py index 7e9c810..71bcff4 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ """GitMon""" -from loguru import logger from gitmon.api import GitHubApi +from gitmon.logger import setup_logger from config import token @@ -15,7 +15,7 @@ def print_event(event): def main(): """Main entry point.""" - + setup_logger() api = GitHubApi(token) events = api.get_events()