From ace7b895410f8d3e42f01a22ab9e594f4c541f8a Mon Sep 17 00:00:00 2001 From: Klafyvel Date: Mon, 18 Dec 2017 15:09:51 +0100 Subject: [PATCH] logger --- main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index b34fa8c..19461b4 100644 --- a/main.py +++ b/main.py @@ -33,6 +33,8 @@ import datetime import httplib2 import os import requests +import logging +from logging.handlers import RotatingFileHandler from apiclient import discovery from oauth2client import client @@ -41,6 +43,45 @@ from oauth2client.file import Storage import icalendar +DEBUG = False +APP_DIR = os.path.dirname(os.path.abspath(__file__)) +if DEBUG: + DATA_DIR = APP_DIR +else: + home_dir = os.path.expanduser('~') + DATA_DIR = os.path.join(home_dir, ".ntnoe", "") + if not os.path.exists(DATA_DIR): + os.makedirs(DATA_DIR) + +##### Logger stuff ##### + +logger = logging.getLogger() + +if DEBUG: + logger.setLevel(logging.DEBUG) +else: + logger.setLevel(logging.INFO) + +formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s') + +file_handler = RotatingFileHandler( + os.path.join(DATA_DIR, 'log.txt'), 'a', 1000000, 1) +if DEBUG: + file_handler.setLevel(logging.DEBUG) +else: + file_handler.setLevel(logging.INFO) + +file_handler.setFormatter(formatter) +logger.addHandler(file_handler) + +if DEBUG: + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.DEBUG) + stream_handler.setFormatter(formatter) + logger.addHandler(stream_handler) + +######################## + TIMEDELTA_SYNCHRO = datetime.timedelta(days=15) # Number of days to look for # for synchronization @@ -112,8 +153,7 @@ def get_credentials(): Returns: Credentials, the obtained credential. """ - home_dir = os.path.expanduser('~') - credential_dir = os.path.join(home_dir, '.credentials') + credential_dir = os.path.join(DATA_DIR, 'credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'ntnoe.json') @@ -123,7 +163,7 @@ def get_credentials(): if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME - print('Storing credentials to ' + credential_path) + logger.info('Storing credentials to ' + credential_path) credentials = tools.run_flow(flow, store) return credentials @@ -158,7 +198,7 @@ def main(): ntnoe_calendar_id = c['id'] if not ntnoe_calendar_id: - print("Creating ntnoe calendar...") + logger.info("Creating ntnoe calendar...") created = service.calendars().insert(body={ 'defaultReminders' : [], 'selected' : True, @@ -175,7 +215,7 @@ def main(): ).execute() for event in former_ones['items']: - print('Deleting event : {}'.format(event['id'])) + logger.info('Deleting event : {}'.format(event['id'])) service.events().delete( calendarId=ntnoe_calendar_id, eventId=event['id'] @@ -196,7 +236,7 @@ def main(): calendarId=ntnoe_calendar_id, body=event.as_google() ).execute() - print("Adding event : {}".format(event['id'])) + logger.info("Adding event : {}".format(event['id'])) if __name__ == '__main__':