Implement basic logging

This commit is contained in:
agatha 2023-11-10 22:33:08 -05:00
parent fb7c7e7bdf
commit a15b5ffda0
3 changed files with 18 additions and 5 deletions

View File

@ -1,10 +1,13 @@
"""GitHub API module""" """GitHub API module"""
import logging
import requests import requests
from datetime import datetime from datetime import datetime
from time import sleep from time import sleep
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry from requests.packages.urllib3.util.retry import Retry
from loguru import logger
from .logger import setup_logger
setup_logger()
class GitHubApi: class GitHubApi:
@ -32,14 +35,14 @@ class GitHubApi:
# Check rate limits # Check rate limits
if self.rate_remaining == 0 and self.rate_reset > datetime.now(): 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() delta = self.rate_reset - datetime.now()
sleep(delta.total_seconds()) sleep(delta.total_seconds())
try: try:
response = self.session.get(url, params=params) response = self.session.get(url, params=params)
except requests.RequestException as e: 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 return None
# 304 Not Modified # 304 Not Modified
@ -53,6 +56,8 @@ class GitHubApi:
"""Parse headers to update rate limits.""" """Parse headers to update rate limits."""
self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset'])) self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset']))
self.rate_remaining = int(headers['x-ratelimit-remaining']) 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): def get_events(self, page=None):
"""Fetch GitHub events.""" """Fetch GitHub events."""

8
gitmon/logger.py Normal file
View File

@ -0,0 +1,8 @@
"""Logging module."""
import sys
from loguru import logger
def setup_logger():
logger.remove()
logger.add(sys.stdout, level='INFO')

View File

@ -1,6 +1,6 @@
"""GitMon""" """GitMon"""
from loguru import logger
from gitmon.api import GitHubApi from gitmon.api import GitHubApi
from gitmon.logger import setup_logger
from config import token from config import token
@ -15,7 +15,7 @@ def print_event(event):
def main(): def main():
"""Main entry point.""" """Main entry point."""
setup_logger()
api = GitHubApi(token) api = GitHubApi(token)
events = api.get_events() events = api.get_events()