Browse Source

Merge pull request #20 from hastic/configurable-logging-level-#19

Configurable logging level #19
master
Coin de Gamma 4 years ago committed by GitHub
parent
commit
a1da5e12ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      analytics/config.py
  2. 6
      analytics/server.py
  3. 2
      analytics/services/server_service.py

27
analytics/config.py

@ -11,20 +11,31 @@ if config_exists:
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 default_val is not None:
return default_val
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 value is None:
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
analytics/server.py

@ -18,6 +18,7 @@ server_service: services.ServerService = None
data_service: services.DataService = None
analytic_unit_manager: AnalyticUnitManager = None
logging.root.setLevel(config.LOGGING_LEVEL)
logger = logging.getLogger('SERVER')
@ -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
analytics/services/server_service.py

@ -111,7 +111,7 @@ class ServerService(utils.concurrent.AsyncZmqActor):
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()

Loading…
Cancel
Save