An error occurred while loading the file. Please try again.
-
Olivier Kaufmann authored82ddc437
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import json
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
from OhmPi.mqtt_handler import MQTTHandler
from OhmPi.compressed_sized_timed_rotating_handler import CompressedSizedTimedRotatingFileHandler
import sys
from termcolor import colored
def create_stdout_logger(name):
logger = logging.getLogger(f'{name}_logger')
log_format = f'%(asctime)-15s | {name[:4]:4s} | %(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)
logger.setLevel(logging.DEBUG)
return logger
def setup_loggers(mqtt=True):
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'])
exec_logger = logging.getLogger('exec_logger')
# 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'
logging_to_console = EXEC_LOGGING_CONFIG['logging_to_console']
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
exec_formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC'
exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(exec_handler)
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)
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']]
mqtt_settings.update({'topic': MQTT_LOGGING_CONFIG['exec_topic']})
# 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)
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')
except Exception as e:
msg += colored(f'\nWarning: Unable to connect to exec topic on broker\n{e}', 'yellow')
mqtt = False
# 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'])
data_formatter = logging.Formatter(log_format)
data_formatter.converter = gmtime
data_formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC'
data_handler.setFormatter(exec_formatter)
data_logger.addHandler(data_handler)
data_logger.setLevel(DATA_LOGGING_CONFIG['logging_level'])
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)
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']})
try:
mqtt_data_handler = MQTTHandler(**mqtt_settings)
mqtt_data_handler.setLevel(MQTT_LOGGING_CONFIG['data_logging_level'])
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')
except Exception as e:
msg += colored(f'\nWarning: Unable to connect to data topic on broker\n{e}', 'yellow')
mqtt = False
try:
init_logging(exec_logger, data_logger, EXEC_LOGGING_CONFIG['logging_level'], log_path, data_log_filename)
except Exception as err:
msg += colored(f'\n\u26A0 ERROR: Could not initialize logging!\n{err}', 'red')
finally:
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')
except Exception as e: # noqa
exec_logger.debug('Unable to get remaining disk space: {e}')
exec_logger.info('Saving data log to ' + data_log_filename)
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)}
for k, v in config_dict.items():
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.')
data_logger.info('Starting_session')
return init_logging_status