Parcourir la source

Implement logging

JoostSijm il y a 5 ans
Parent
commit
fd0cfe9b65
3 fichiers modifiés avec 15 ajouts et 8 suppressions
  1. 7 0
      app/__init__.py
  2. 4 3
      app/__main__.py
  3. 4 5
      app/api.py

+ 7 - 0
app/__init__.py

@@ -1,6 +1,7 @@
 """Init"""
 
 import os
+import logging
 
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
@@ -20,6 +21,12 @@ Session = sessionmaker(bind=engine)
 scheduler = BackgroundScheduler()
 scheduler.start()
 
+logging.basicConfig(
+    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+    level=logging.INFO
+)
+LOGGER = logging.getLogger(__name__)
+
 # api
 BASE_URL = os.environ["API_URL"]
 HEADERS = {

+ 4 - 3
app/__main__.py

@@ -4,7 +4,7 @@ from datetime import datetime, timedelta
 import random
 import time
 
-from app import scheduler
+from app import scheduler, LOGGER
 from app.api import download_resources, refill
 from app.database import save_resources
 from app.app import need_refill, max_refill_seconds, print_resources
@@ -21,11 +21,12 @@ def job_check_resources(state_id, capital_id, resource_id, do_refill):
         random_time_delta = timedelta(seconds=random_seconds)
         scheduled_date = datetime.now() + random_time_delta
         job_id = 'refill_{}_{}'.format(capital_id, resource_id)
-        print('refill resource: {} at {} ({} minutes)'.format(
+        LOGGER.info(
+            'Refil resource %s at %s (%s minutes)',
             resource_id,
             scheduled_date,
             round(random_time_delta.seconds / 60)
-        ))
+        )
         job = scheduler.get_job(job_id)
         if not job:
             scheduler.add_job(

+ 4 - 5
app/api.py

@@ -5,7 +5,7 @@ import re
 import requests
 from bs4 import BeautifulSoup
 
-from app import BASE_URL, HEADERS
+from app import BASE_URL, HEADERS, LOGGER
 
 
 RESOURCES = {
@@ -62,7 +62,7 @@ def refill(state_id, capital_id, resource_id):
     state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
     action = state_div.findChild()['action']
     current_state_id = int(re.sub('.*/', '', action))
-    print('Current state: {}'.format(current_state_id))
+    LOGGER.info('Current state %s', current_state_id)
 
     data = {
         'tmp_gov': resource_id
@@ -88,8 +88,7 @@ def refill(state_id, capital_id, resource_id):
     exploration_laws = active_laws.findAll(
         text='Resources exploration: state, {} resources'.format(resource_name)
     )
-    print('Resources exploration: state, {} resources'.format(resource_name))
-    print(exploration_laws)
+    LOGGER.info('Resources exploration: state, %s resources', resource_name)
     for exploration_law in exploration_laws:
         action = exploration_law.parent.parent['action']
         action = action.replace('law', 'votelaw')
@@ -97,4 +96,4 @@ def refill(state_id, capital_id, resource_id):
             '{}{}/pro'.format(BASE_URL, action),
             headers=HEADERS
         )
-        print(result.text)
+        LOGGER.info('Response %s', result.text)