From 955409d81aa946e84ba024601188d7fe5c6b91e0 Mon Sep 17 00:00:00 2001 From: prash3r Date: Tue, 16 May 2023 12:23:24 +0200 Subject: [PATCH] repaires log clearence from analytics page --- .gitignore | 3 ++- .vscode/launch.json | 6 +++++- cbpi/controller/log_file_controller.py | 6 ++++++ cbpi/extension/SensorLogTarget_CSV/__init__.py | 7 +++---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d23da45..6157d3e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ logs/ .coverage .devcontainer/cbpi-dev-config/* cbpi4-* -temp* \ No newline at end of file +temp* +*.patch \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index e24e1e9..0b0fdc4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,11 @@ "type": "python", "request": "launch", "module": "run", - "args": ["--config-folder-path=./.devcontainer/cbpi-dev-config", "start"], + "args": [ + "--config-folder-path=./.devcontainer/cbpi-dev-config", + "--debug-log-level=20", + "start" + ], "preLaunchTask": "copy default cbpi config files if dev config files dont exist" }, diff --git a/cbpi/controller/log_file_controller.py b/cbpi/controller/log_file_controller.py index 14a9c02..fecae47 100644 --- a/cbpi/controller/log_file_controller.py +++ b/cbpi/controller/log_file_controller.py @@ -151,6 +151,12 @@ class LogController: def clear_log(self, name:str ) -> str: all_filenames = glob.glob(os.path.join(self.logsFolderPath, f"sensor_{name}.log*")) + + logging.info(f'Deleting logfiles for sensor {name}.') + + if name in self.datalogger: + self.datalogger[name].removeHandler(self.datalogger[name].handlers[0]) + del self.datalogger[name] for f in all_filenames: try: diff --git a/cbpi/extension/SensorLogTarget_CSV/__init__.py b/cbpi/extension/SensorLogTarget_CSV/__init__.py index 8fbb468..6360cf7 100644 --- a/cbpi/extension/SensorLogTarget_CSV/__init__.py +++ b/cbpi/extension/SensorLogTarget_CSV/__init__.py @@ -17,7 +17,6 @@ class SensorLogTargetCSV(CBPiExtension): def __init__(self, cbpi): # called from cbpi on start self.cbpi = cbpi - self.datalogger = {} self.logfiles = self.cbpi.config.get("CSVLOGFILES", "Yes") if self.logfiles == "No": return # never run() @@ -35,7 +34,7 @@ class SensorLogTargetCSV(CBPiExtension): # as long as cbpi was STARTED with CSVLOGFILES set to Yes this function is still subscribed, so changes can be made on the fly. # but after initially enabling this logging target a restart is required. return - if id not in self.datalogger: + if id not in self.cbpi.log.datalogger: max_bytes = int(self.cbpi.config.get("SENSOR_LOG_MAX_BYTES", 100000)) backup_count = int(self.cbpi.config.get("SENSOR_LOG_BACKUP_COUNT", 3)) @@ -44,9 +43,9 @@ class SensorLogTargetCSV(CBPiExtension): data_logger.setLevel(logging.DEBUG) handler = RotatingFileHandler(os.path.join(self.cbpi.log.logsFolderPath, f"sensor_{id}.log"), maxBytes=max_bytes, backupCount=backup_count) data_logger.addHandler(handler) - self.datalogger[id] = data_logger + self.cbpi.log.datalogger[id] = data_logger - self.datalogger[id].info("%s,%s" % (formatted_time, str(value))) + self.cbpi.log.datalogger[id].info("%s,%s" % (formatted_time, str(value))) def setup(cbpi): cbpi.plugin.register("SensorLogTargetCSV", SensorLogTargetCSV)