Skip to content

Commit

Permalink
Merge pull request #20 from hastic/configurable-logging-level-#19
Browse files Browse the repository at this point in the history
Configurable logging level #19
  • Loading branch information
jonyrock committed Jun 22, 2020
2 parents 4b363ac + d4d3e28 commit a1da5e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
27 changes: 19 additions & 8 deletions analytics/config.py
Expand Up @@ -11,20 +11,31 @@
with open(CONFIG_FILE) as f:
config = json.load(f)
else:
print('Config file %s doesn`t exist, using defaults' % CONFIG_FILE)
print('Config file %s doesn`t exist, using environment variables / defaults' % CONFIG_FILE)


def get_config_field(field: str, default_val = None):
def get_config_field(field: str, default_val = None, allowed_values = []):
value = None
if field in os.environ:
return os.environ[field]
value = os.environ[field]
elif config_exists and field in config and config[field] != '':
value = config[field]
elif default_val is not None:
value = default_val

if config_exists and field in config and config[field] != '':
return config[field]
if len(allowed_values) > 0 and value not in allowed_values:
raise Exception('{} value must be one of: {}, got: {}'.format(field, allowed_values, value))

if default_val is not None:
return default_val
if value is None:
raise Exception('Please configure {}'.format(field))

raise Exception('Please configure {}'.format(field))
return value

HASTIC_SERVER_URL = get_config_field('HASTIC_SERVER_URL', 'ws://localhost:8002')
LOGGING_LEVEL = get_config_field(
'HS_AN_LOGGING_LEVEL',
'INFO',
# TODO: make values case insensitive
['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
)
LEARNING_TIMEOUT = get_config_field('LEARNING_TIMEOUT', 120)
6 changes: 3 additions & 3 deletions analytics/server.py
Expand Up @@ -18,6 +18,7 @@
data_service: services.DataService = None
analytic_unit_manager: AnalyticUnitManager = None

logging.root.setLevel(config.LOGGING_LEVEL)
logger = logging.getLogger('SERVER')


Expand Down Expand Up @@ -85,10 +86,9 @@ async def app_loop():

def run_server():
loop = asyncio.get_event_loop()
#loop.set_debug(True)

logger.info("Ok")
init_services()
print('Analytics process is running') # we need to print to stdout and flush
sys.stdout.flush() # because node.js expects it
logger.info('Analytics process is running')

loop.run_until_complete(app_loop())
2 changes: 1 addition & 1 deletion analytics/services/server_service.py
Expand Up @@ -111,7 +111,7 @@ async def __reconnect_recv(self) -> str:
self.__server_socket = None
# TODO: move to config
reconnect_delay = 3
print('connection is refused or lost, trying to reconnect in %s seconds' % reconnect_delay)
logger.info('connection is refused or lost, trying to reconnect in %s seconds' % reconnect_delay)
await asyncio.sleep(reconnect_delay)
raise InterruptedError()

Expand Down

0 comments on commit a1da5e1

Please sign in to comment.