|
1 | 1 | import functools |
2 | 2 | import logging |
| 3 | +import logging.handlers |
3 | 4 | from logging.handlers import RotatingFileHandler |
4 | 5 | from pathlib import Path |
| 6 | +import queue |
5 | 7 | import sys |
6 | 8 | import threading |
7 | 9 | import typing_extensions |
@@ -106,72 +108,119 @@ def filter_pos(name: str, record) -> bool: |
106 | 108 | def setup_logging() -> None: |
107 | 109 | def mb_to_bytes(megabytes: int) -> int: |
108 | 110 | return megabytes * 1000000 |
109 | | - # Mehrere kleine Dateien verwenden, damit nicht zu viel verworfen wird, wenn die Datei voll ist. |
| 111 | + |
| 112 | + # Main logger |
| 113 | + log_queue = queue.Queue() |
| 114 | + queue_handler = logging.handlers.QueueHandler(log_queue) |
110 | 115 | main_file_handler = RotatingFileHandler(RAMDISK_PATH + 'main.log', maxBytes=mb_to_bytes(5.5), backupCount=4) |
111 | 116 | main_file_handler.setFormatter(logging.Formatter(FORMAT_STR_DETAILED)) |
112 | 117 | main_file_handler.addFilter(RedactingFilter()) |
113 | | - logging.basicConfig(level=logging.DEBUG, handlers=[main_file_handler]) |
| 118 | + logging.basicConfig(level=logging.DEBUG, handlers=[queue_handler]) |
114 | 119 | logging.getLogger().handlers[0].addFilter(functools.partial(filter_neg, "soc")) |
115 | 120 | logging.getLogger().handlers[0].addFilter(functools.partial(filter_neg, "Internal Chargepoint")) |
116 | 121 | logging.getLogger().handlers[0].addFilter(functools.partial(filter_neg, "smarthome")) |
| 122 | + main_listener = logging.handlers.QueueListener(log_queue, main_file_handler) |
| 123 | + main_listener.start() |
117 | 124 |
|
| 125 | + # Chargelog logger |
| 126 | + chargelog_queue = queue.Queue() |
| 127 | + chargelog_queue_handler = logging.handlers.QueueHandler(chargelog_queue) |
118 | 128 | chargelog_log = logging.getLogger("chargelog") |
119 | 129 | chargelog_log.propagate = False |
120 | 130 | chargelog_file_handler = RotatingFileHandler( |
121 | 131 | RAMDISK_PATH + 'chargelog.log', maxBytes=mb_to_bytes(2), backupCount=1) |
122 | 132 | chargelog_file_handler.setFormatter(logging.Formatter(FORMAT_STR_SHORT)) |
123 | 133 | chargelog_file_handler.addFilter(RedactingFilter()) |
124 | | - chargelog_log.addHandler(chargelog_file_handler) |
| 134 | + chargelog_log.addHandler(chargelog_queue_handler) |
| 135 | + chargelog_listener = logging.handlers.QueueListener(chargelog_queue, chargelog_file_handler) |
| 136 | + chargelog_listener.start() |
125 | 137 |
|
| 138 | + # Data migration logger |
| 139 | + data_migration_queue = queue.Queue() |
| 140 | + data_migration_queue_handler = logging.handlers.QueueHandler(data_migration_queue) |
126 | 141 | data_migration_log = logging.getLogger("data_migration") |
127 | 142 | data_migration_log.propagate = False |
128 | 143 | data_migration_file_handler = RotatingFileHandler( |
129 | 144 | PERSISTENT_LOG_PATH + 'data_migration.log', maxBytes=mb_to_bytes(1), backupCount=1) |
130 | 145 | data_migration_file_handler.setFormatter(logging.Formatter(FORMAT_STR_SHORT)) |
131 | 146 | data_migration_file_handler.addFilter(RedactingFilter()) |
132 | | - data_migration_log.addHandler(data_migration_file_handler) |
| 147 | + data_migration_log.addHandler(data_migration_queue_handler) |
| 148 | + data_migration_listener = logging.handlers.QueueListener(data_migration_queue, data_migration_file_handler) |
| 149 | + data_migration_listener.start() |
133 | 150 |
|
| 151 | + # MQTT logger |
| 152 | + mqtt_queue = queue.Queue() |
| 153 | + mqtt_queue_handler = logging.handlers.QueueHandler(mqtt_queue) |
134 | 154 | mqtt_log = logging.getLogger("mqtt") |
135 | 155 | mqtt_log.propagate = False |
136 | 156 | mqtt_file_handler = RotatingFileHandler(RAMDISK_PATH + 'mqtt.log', maxBytes=mb_to_bytes(3), backupCount=1) |
137 | 157 | mqtt_file_handler.setFormatter(logging.Formatter(FORMAT_STR_SHORT)) |
138 | 158 | mqtt_file_handler.addFilter(RedactingFilter()) |
139 | | - mqtt_log.addHandler(mqtt_file_handler) |
| 159 | + mqtt_log.addHandler(mqtt_queue_handler) |
| 160 | + mqtt_listener = logging.handlers.QueueListener(mqtt_queue, mqtt_file_handler) |
| 161 | + mqtt_listener.start() |
140 | 162 |
|
| 163 | + # Steuve control command logger |
| 164 | + steuve_control_command_queue = queue.Queue() |
| 165 | + steuve_control_command_queue_handler = logging.handlers.QueueHandler(steuve_control_command_queue) |
141 | 166 | steuve_control_command_log = logging.getLogger("steuve_control_command") |
142 | 167 | steuve_control_command_log.propagate = False |
143 | 168 | steuve_control_command_file_handler = RotatingFileHandler( |
144 | 169 | PERSISTENT_LOG_PATH + 'steuve_control_command.log', maxBytes=mb_to_bytes(80), backupCount=1) |
145 | 170 | steuve_control_command_file_handler.setFormatter(logging.Formatter(FORMAT_STR_SHORT)) |
146 | | - steuve_control_command_log.addHandler(steuve_control_command_file_handler) |
147 | | - |
| 171 | + steuve_control_command_log.addHandler(steuve_control_command_queue_handler) |
| 172 | + steuve_control_command_listener = logging.handlers.QueueListener(steuve_control_command_queue, |
| 173 | + steuve_control_command_file_handler) |
| 174 | + steuve_control_command_listener.start() |
| 175 | + |
| 176 | + # Smarthome logger |
| 177 | + smarthome_queue = queue.Queue() |
| 178 | + smarthome_queue_handler = logging.handlers.QueueHandler(smarthome_queue) |
148 | 179 | smarthome_log_handler = RotatingFileHandler(RAMDISK_PATH + 'smarthome.log', maxBytes=mb_to_bytes(1), backupCount=1) |
149 | 180 | smarthome_log_handler.setFormatter(logging.Formatter(FORMAT_STR_SHORT)) |
150 | 181 | smarthome_log_handler.addFilter(functools.partial(filter_pos, "smarthome")) |
151 | 182 | smarthome_log_handler.addFilter(RedactingFilter()) |
152 | | - logging.getLogger().addHandler(smarthome_log_handler) |
| 183 | + logging.getLogger().addHandler(smarthome_queue_handler) |
| 184 | + smarthome_listener = logging.handlers.QueueListener(smarthome_queue, smarthome_log_handler) |
| 185 | + smarthome_listener.start() |
153 | 186 |
|
| 187 | + # SoC logger |
| 188 | + soc_queue = queue.Queue() |
| 189 | + soc_queue_handler = logging.handlers.QueueHandler(soc_queue) |
154 | 190 | soc_log_handler = RotatingFileHandler(RAMDISK_PATH + 'soc.log', maxBytes=mb_to_bytes(2), backupCount=1) |
155 | 191 | soc_log_handler.setFormatter(logging.Formatter(FORMAT_STR_DETAILED)) |
156 | 192 | soc_log_handler.addFilter(functools.partial(filter_pos, "soc")) |
157 | 193 | soc_log_handler.addFilter(RedactingFilter()) |
158 | | - logging.getLogger().addHandler(soc_log_handler) |
| 194 | + logging.getLogger().addHandler(soc_queue_handler) |
| 195 | + soc_listener = logging.handlers.QueueListener(soc_queue, soc_log_handler) |
| 196 | + soc_listener.start() |
159 | 197 |
|
| 198 | + # Internal chargepoint logger |
| 199 | + internal_chargepoint_queue = queue.Queue() |
| 200 | + internal_chargepoint_queue_handler = logging.handlers.QueueHandler(internal_chargepoint_queue) |
160 | 201 | internal_chargepoint_log_handler = RotatingFileHandler(RAMDISK_PATH + 'internal_chargepoint.log', |
161 | 202 | maxBytes=mb_to_bytes(1), |
162 | 203 | backupCount=1) |
163 | 204 | internal_chargepoint_log_handler.setFormatter(logging.Formatter(FORMAT_STR_DETAILED)) |
164 | 205 | internal_chargepoint_log_handler.addFilter(functools.partial(filter_pos, "Internal Chargepoint")) |
165 | 206 | internal_chargepoint_log_handler.addFilter(RedactingFilter()) |
166 | | - logging.getLogger().addHandler(internal_chargepoint_log_handler) |
167 | | - |
| 207 | + logging.getLogger().addHandler(internal_chargepoint_queue_handler) |
| 208 | + internal_chargepoint_listener = logging.handlers.QueueListener(internal_chargepoint_queue, |
| 209 | + internal_chargepoint_log_handler) |
| 210 | + internal_chargepoint_listener.start() |
| 211 | + |
| 212 | + # urllib3 logger |
| 213 | + urllib3_queue = queue.Queue() |
| 214 | + urllib3_queue_handler = logging.handlers.QueueHandler(urllib3_queue) |
168 | 215 | urllib3_log = logging.getLogger("urllib3.connectionpool") |
169 | 216 | urllib3_log.propagate = True |
170 | 217 | urllib3_file_handler = RotatingFileHandler(RAMDISK_PATH + 'soc.log', maxBytes=mb_to_bytes(2), backupCount=1) |
171 | 218 | urllib3_file_handler.setFormatter(logging.Formatter(FORMAT_STR_DETAILED)) |
172 | 219 | urllib3_file_handler.addFilter(RedactingFilter()) |
173 | 220 | urllib3_file_handler.addFilter(functools.partial(filter_pos, "soc")) |
174 | | - urllib3_log.addHandler(urllib3_file_handler) |
| 221 | + urllib3_log.addHandler(urllib3_queue_handler) |
| 222 | + urllib3_listener = logging.handlers.QueueListener(urllib3_queue, urllib3_file_handler) |
| 223 | + urllib3_listener.start() |
175 | 224 |
|
176 | 225 | logging.getLogger("pymodbus").setLevel(logging.WARNING) |
177 | 226 | logging.getLogger("uModbus").setLevel(logging.WARNING) |
|
0 commit comments