Newer
Older
Olivier Kaufmann
committed
import json
Olivier Kaufmann
committed
from OhmPi.config import EXEC_LOGGING_CONFIG, DATA_LOGGING_CONFIG, MQTT_LOGGING_CONFIG, MQTT_CONTROL_CONFIG
from os import path, mkdir, statvfs
from time import gmtime
import logging
Olivier Kaufmann
committed
from OhmPi.mqtt_handler import MQTTHandler
from OhmPi.compressed_sized_timed_rotating_handler import CompressedSizedTimedRotatingFileHandler
Olivier Kaufmann
committed
from termcolor import colored
def create_default_logger(name):
logger = logging.getLogger(f'{name}_logger')
log_format = f'%(asctime)-15s | {name} | %(levelname)s: %(message)s'
formatter = logging.Formatter(log_format)
formatter.converter = gmtime
formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC'
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
Olivier Kaufmann
committed
logger.setLevel(logging.DEBUG)
Olivier Kaufmann
committed
def setup_loggers(mqtt=True):
Olivier Kaufmann
committed
msg = ''
# Message logging setup
log_path = path.join(path.dirname(__file__), 'logs')
if not path.isdir(log_path):
mkdir(log_path)
exec_log_filename = path.join(log_path, EXEC_LOGGING_CONFIG['file_name'])
Olivier Kaufmann
committed
exec_logger = logging.getLogger('exec_logger')
Olivier Kaufmann
committed
# SOH logging setup
# TODO: Add state of health logging here
# Data logging setup
base_path = path.dirname(__file__)
data_path = path.join(base_path, 'data')
if not path.isdir(data_path):
mkdir(data_path)
data_log_filename = path.join(data_path, DATA_LOGGING_CONFIG['file_name'])
data_logger = logging.getLogger('data_logger')
# Set message logging format and level
log_format = '%(asctime)-15s | %(process)d | %(levelname)s: %(message)s'
Olivier Kaufmann
committed
logging_to_console = EXEC_LOGGING_CONFIG['logging_to_console']
Olivier Kaufmann
committed
exec_handler = CompressedSizedTimedRotatingFileHandler(exec_log_filename,
max_bytes=EXEC_LOGGING_CONFIG['max_bytes'],
backup_count=EXEC_LOGGING_CONFIG['backup_count'],
when=EXEC_LOGGING_CONFIG['when'],
interval=EXEC_LOGGING_CONFIG['interval'])
exec_formatter = logging.Formatter(log_format)
exec_formatter.converter = gmtime
Olivier Kaufmann
committed
exec_formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC'
exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(exec_handler)
Olivier Kaufmann
committed
exec_logger.setLevel(EXEC_LOGGING_CONFIG['log_file_logging_level'])
if logging_to_console:
console_exec_handler = logging.StreamHandler(sys.stdout)
console_exec_handler.setLevel(EXEC_LOGGING_CONFIG['logging_level'])
console_exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(console_exec_handler)
Olivier Kaufmann
committed
if mqtt:
mqtt_settings = MQTT_LOGGING_CONFIG.copy()
mqtt_exec_logging_level = mqtt_settings.pop('exec_logging_level', logging.DEBUG)
[mqtt_settings.pop(i) for i in ['client_id', 'exec_topic', 'data_topic', 'soh_topic', 'data_logging_level',
'soh_logging_level']]
Olivier Kaufmann
committed
mqtt_settings.update({'topic': MQTT_LOGGING_CONFIG['exec_topic']})
Olivier Kaufmann
committed
# TODO: handle the case of MQTT broker down or temporarily unavailable
try:
mqtt_exec_handler = MQTTHandler(**mqtt_settings)
mqtt_exec_handler.setLevel(mqtt_exec_logging_level)
Olivier Kaufmann
committed
mqtt_exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(mqtt_exec_handler)
msg += colored(f"\n\u2611 Publishes execution as {MQTT_LOGGING_CONFIG['exec_topic']} topic on the "
f"{MQTT_LOGGING_CONFIG['hostname']} broker", 'blue')
Olivier Kaufmann
committed
except Exception as e:
msg += colored(f'\nWarning: Unable to connect to exec topic on broker\n{e}', 'yellow')
Olivier Kaufmann
committed
mqtt = False
Olivier Kaufmann
committed
# Set data logging format and level
log_format = '%(asctime)-15s | %(process)d | %(levelname)s: %(message)s'
logging_to_console = DATA_LOGGING_CONFIG['logging_to_console']
data_handler = CompressedSizedTimedRotatingFileHandler(data_log_filename,
max_bytes=DATA_LOGGING_CONFIG['max_bytes'],
backup_count=DATA_LOGGING_CONFIG['backup_count'],
when=DATA_LOGGING_CONFIG['when'],
interval=DATA_LOGGING_CONFIG['interval'])
Olivier Kaufmann
committed
data_formatter = logging.Formatter(log_format)
data_formatter.converter = gmtime
data_formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC'
Olivier Kaufmann
committed
data_handler.setFormatter(exec_formatter)
data_logger.addHandler(data_handler)
data_logger.setLevel(DATA_LOGGING_CONFIG['logging_level'])
Olivier Kaufmann
committed
if logging_to_console:
console_data_handler = logging.StreamHandler(sys.stdout)
console_data_handler.setLevel(DATA_LOGGING_CONFIG['logging_level'])
console_data_handler.setFormatter(exec_formatter)
data_logger.addHandler(console_data_handler)
Olivier Kaufmann
committed
if mqtt:
mqtt_settings = MQTT_LOGGING_CONFIG.copy()
mqtt_data_logging_level = mqtt_settings.pop('data_logging_level', logging.INFO)
[mqtt_settings.pop(i) for i in ['client_id', 'exec_topic', 'data_topic', 'soh_topic', 'exec_logging_level',
'soh_logging_level']]
mqtt_settings.update({'topic': MQTT_LOGGING_CONFIG['data_topic']})
Olivier Kaufmann
committed
try:
mqtt_data_handler = MQTTHandler(**mqtt_settings)
mqtt_data_handler.setLevel(MQTT_LOGGING_CONFIG['data_logging_level'])
Olivier Kaufmann
committed
mqtt_data_handler.setFormatter(data_formatter)
data_logger.addHandler(mqtt_data_handler)
msg += colored(f"\n\u2611 Publishes data as {MQTT_LOGGING_CONFIG['data_topic']} topic on the "
f"{MQTT_LOGGING_CONFIG['hostname']} broker", 'blue')
Olivier Kaufmann
committed
except Exception as e:
msg += colored(f'\nWarning: Unable to connect to data topic on broker\n{e}', 'yellow')
mqtt = False
Olivier Kaufmann
committed
try:
init_logging(exec_logger, data_logger, EXEC_LOGGING_CONFIG['logging_level'], log_path, data_log_filename)
Olivier Kaufmann
committed
except Exception as err:
Olivier Kaufmann
committed
msg += colored(f'\n\u26A0 ERROR: Could not initialize logging!\n{err}', 'red')
Olivier Kaufmann
committed
finally:
Olivier Kaufmann
committed
return exec_logger, exec_log_filename, data_logger, data_log_filename, EXEC_LOGGING_CONFIG['logging_level'], msg
def init_logging(exec_logger, data_logger, exec_logging_level, log_path, data_log_filename):
""" This is the init sequence for the logging system """
init_logging_status = True
exec_logger.info('')
exec_logger.info('****************************')
exec_logger.info('*** NEW SESSION STARTING ***')
exec_logger.info('****************************')
exec_logger.info('')
exec_logger.debug(f'Logging level: {exec_logging_level}')
try:
st = statvfs('.')
available_space = st.f_bavail * st.f_frsize / 1024 / 1024
exec_logger.info(f'Remaining disk space : {available_space:.1f} MB')
exec_logger.debug('Unable to get remaining disk space: {e}')
exec_logger.info('Saving data log to ' + data_log_filename)
Olivier Kaufmann
committed
config_dict = {'execution logging configuration': json.dumps(EXEC_LOGGING_CONFIG, indent=4),
'data logging configuration': json.dumps(DATA_LOGGING_CONFIG, indent=4),
'mqtt logging configuration': json.dumps(MQTT_LOGGING_CONFIG, indent=4),
'mqtt control configuration': json.dumps(MQTT_CONTROL_CONFIG, indent=4)}
Olivier Kaufmann
committed
for k, v in config_dict.items():
Olivier Kaufmann
committed
exec_logger.debug(f'{k}:\n{v}')
exec_logger.debug('')
if not init_logging_status:
exec_logger.warning(f'Logging initialisation has encountered a problem.')
Olivier Kaufmann
committed
data_logger.info('Starting_session')
return init_logging_status