From bbf7726ca43bdecf667c368808a9c18c848b7c2e Mon Sep 17 00:00:00 2001 From: EUNIX-TRIX Date: Sun, 8 Apr 2018 23:42:49 +0530 Subject: [PATCH] Added log.py and implemented logging functionality --- app/log.py | 10 ++++++++++ app/main.py | 27 +++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 app/log.py diff --git a/app/log.py b/app/log.py new file mode 100644 index 0000000..da7aaf9 --- /dev/null +++ b/app/log.py @@ -0,0 +1,10 @@ +import logging + +def setup_custom_logger(name): + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) + formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s') + file_handler = logging.FileHandler('main.log') + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + return logger \ No newline at end of file diff --git a/app/main.py b/app/main.py index 6eef754..320b055 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,3 @@ -import logging -logging.basicConfig(filename='logger.log', level=logging.INFO, - format='%(asctime)s:%(levelname)s:%(name)s:%(message)s' ) import pyttsx3 as tts # For Text to Speech import speech_recognition as stt # For Speech to Text @@ -10,6 +7,8 @@ from app.config import tts_config from app.config import stt_config +import app.log as app_logger +logger = app_logger.setup_custom_logger('root') def start_app(): @@ -25,7 +24,7 @@ def start_app(): device_id = 0 # first device (microphone) # Get Microphone Device ID - print('Available Audio Devices: ') + logger.info('Available Audio Devices: ') mic_list = stt.Microphone.list_microphone_names() for i, micName in enumerate(mic_list): print(i, micName) @@ -33,22 +32,22 @@ def start_app(): try: device_id = int(input('Microphone ID: ')) except Exception as e: - logging.info('Invalid Driver ID, using 0') + logger.error('Invalid Driver ID, using 0') finally: device_id = 0 if device_id < 0 or device_id >= len(mic_list) else device_id try: with stt.Microphone(device_index=device_id, sample_rate=sample_rate, chunk_size=chunk_size) as source: recognizer.adjust_for_ambient_noise(source) # removing noise - logging.info("Ready.") + logger.info("Ready.") while True: # STT Loop audio = recognizer.listen(source) # listen from mic try: text = recognizer.recognize_google(audio) # recognize with google - logging.info("You said:") - logging.info(text) + logger.info("You said:") + logger.info(text) text = text.lower() # Fetch Reply @@ -66,17 +65,17 @@ def start_app(): reply.action(text) # Perform Action output = reply.getReply(text) # Generate Reply - logging.info(output) #logging Reply + logger.info(output) #logger Reply engine.say(output) # TTS engine.runAndWait() # Wait for Speech To Complete except stt.UnknownValueError as e: - logging.info("Google Speech Recognition could not understand audio") + logger.error("Google Speech Recognition could not understand audio") except stt.RequestError as e: - logging.info("Could not request results from Google Speech Recognition service; {0}".format(e)) + logger.error("Could not request results from Google Speech Recognition service; {0}".format(e)) except Exception as e: - logging.info(e) + logger.warn(e) except Exception as e: - logging.info("Exception Occurred") - logging.info(e) + logger.warn("Exception Occurred") + logger.warn(e)