Skip to content

Commit bea3756

Browse files
authored
Merge pull request ggeop#14 from ggeop/logging_dev
Logging dev
2 parents daf9a5e + 8aa257e commit bea3756

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

src/jarvis/config.conf

-22
This file was deleted.

src/jarvis/jarvis/assistant_utils.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import sys
22
import os
33
import traceback
4-
import logging.config
4+
import logging
5+
from logging import config
56
from google_speech import Speech
67
from subprocess import call
7-
from jarvis.settings import GOOGLE_SPEECH
88

9-
from jarvis.settings import GENERAL_SETTINGS
9+
from jarvis.settings import GOOGLE_SPEECH, GENERAL_SETTINGS, LOG_SETTINGS
1010

1111

1212
Jarvis_logo = ""\
@@ -29,27 +29,21 @@ class OutputStyler:
2929
UNDERLINE = '\033[4m'
3030

3131

32-
# Relative path to configuration file
33-
jarvis_path = os.path.dirname(os.path.abspath(__file__))
34-
config_path = os.path.join(jarvis_path, '..', 'config.conf')
35-
36-
logging.config.fileConfig(fname=config_path, disable_existing_loggers=False)
37-
logger = logging.getLogger(__name__) # Create logger
32+
# Create a Console & Rotating file logger
33+
config.dictConfig(LOG_SETTINGS)
3834

3935

4036
def log(func):
4137
def wrapper(*args, **kwargs):
4238
try:
43-
logger.debug(func.__name__)
39+
logging.debug(func.__name__)
4440
func(*args, **kwargs)
4541
except Exception as e:
46-
logger.error(func.__name__)
42+
logging.error(func.__name__)
4743
traceback.print_exc(file=sys.stdout)
48-
4944
return wrapper
5045

5146

52-
@log
5347
def assistant_response(text):
5448
"""
5549
Assistant response in voice or/and in text
@@ -64,7 +58,7 @@ def assistant_response(text):
6458

6559

6660
def user_speech_playback(text):
67-
user_speech = str(OutputStyler.GREEN + 'You: ' + text)
61+
user_speech = str(OutputStyler.GREEN + 'You: ' + text + '\n')
6862
sys.stdout.write(OutputStyler.BLUE + user_speech + OutputStyler.ENDC)
6963

7064

src/jarvis/jarvis/command_manager.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self):
2121
self.r = sr.Recognizer()
2222
self.words = None
2323

24+
@log
2425
def run(self):
2526
self.words = self._get_words()
2627
commands = self._get_commands()
@@ -37,7 +38,6 @@ def wake_up_check(self):
3738
self.words = self.r.recognize_google(audio).lower()
3839
except sr.UnknownValueError:
3940
self.words = self._get_words()
40-
4141
if TRIGGERING_WORDS['enable_jarvis'] in self.words:
4242
self._wake_up_response()
4343
return True
@@ -51,6 +51,7 @@ def shutdown_check(self):
5151
"""
5252
if TRIGGERING_WORDS['disable_jarvis'] in self.words:
5353
assistant_response('Bye bye Sir. Have a nice day')
54+
logging.debug('Application terminated gracefully.')
5455
sys.exit()
5556

5657
@staticmethod
@@ -78,10 +79,11 @@ def _get_commands(self):
7879
words_set = set(words)
7980
return commands_set.intersection(words_set)
8081

82+
@log
8183
def _execute_commands(self, commands):
8284
"""
8385
Execute iteratively all the commands in the input dict.
84-
:param commands: dict (e.g {'open', 'search'})
86+
:param commands: set (e.g {'open', 'search'})
8587
"""
8688
if bool(commands):
8789
for command in commands:
@@ -112,4 +114,5 @@ def _record(self):
112114
self.r.pause_threshold = SPEECH_RECOGNITION['pause_threshold']
113115
self.r.adjust_for_ambient_noise(source, duration=SPEECH_RECOGNITION['ambient_duration'])
114116
audio_text = self.r.listen(source)
117+
115118
return audio_text

src/jarvis/jarvis/settings.py

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
LOG_SETTINGS = {
2+
'version': 1,
3+
'root': {
4+
'level': 'DEBUG',
5+
'handlers': ['console', 'file'],
6+
},
7+
'handlers': {
8+
'console': {
9+
'class': 'logging.StreamHandler',
10+
'level': 'INFO',
11+
'formatter': 'detailed',
12+
'stream': 'sys.stdout',
13+
},
14+
'file': {
15+
'class': 'logging.handlers.RotatingFileHandler',
16+
'level': 'DEBUG',
17+
'formatter': 'detailed',
18+
'filename': '/var/log/jarvis.log',
19+
'mode': 'a',
20+
'maxBytes': 10000000,
21+
'backupCount': 3,
22+
},
23+
},
24+
'formatters': {
25+
'detailed': {
26+
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
27+
},
28+
}
29+
}
30+
131

232
# General assistant settings
333
GENERAL_SETTINGS = {

0 commit comments

Comments
 (0)