diff --git a/src/config.example.yaml b/src/config.example.yaml index 7e60f64..3fd5ca9 100644 --- a/src/config.example.yaml +++ b/src/config.example.yaml @@ -1,6 +1,12 @@ general: module_data_file: data.yaml +logging: + level: INFO # level at which logging should start + format: + time: "%Y-%m-%d %H:%M.%S" # other example: "iso" + renderer: console # or: json + ceph: conf_file: ../.ceph/ceph.conf keyring: ../.ceph/ceph.client.admin.keyring diff --git a/src/rookify/__main__.py b/src/rookify/__main__.py index 1ea9c40..a4f337a 100644 --- a/src/rookify/__main__.py +++ b/src/rookify/__main__.py @@ -4,18 +4,29 @@ from types import MappingProxyType from .yaml import load_yaml, save_yaml +from rookify.logger import configure_logging, getLogger def main(): + # Load configuration file try: config = load_yaml("config.yaml") except FileNotFoundError as err: raise SystemExit(f'Could not load config: {err}') migration_modules = rookify.modules.load_modules(config['migration_modules']) + # Configure structlog logging + try: + configure_logging(config) + log = getLogger() + log.info("Structlog configured successfully.") + except Exception as e: + raise SystemExit(f'Error configuring logging: {e}') + module_data = dict() try: module_data.update(load_yaml(config['general']['module_data_file'])) except FileNotFoundError: + log.exception("No module_data_file was found, a new one will be generated") pass # Get a list of handlers and run handlers if they should be run in preflight @@ -39,6 +50,7 @@ def main(): module_data[module.__name__] = result save_yaml(config['general']['module_data_file'], module_data) + log.info("Data was updated to module_data_file.") if __name__ == "__main__": main() diff --git a/src/rookify/logger.py b/src/rookify/logger.py new file mode 100644 index 0000000..e3eb21d --- /dev/null +++ b/src/rookify/logger.py @@ -0,0 +1,22 @@ +import structlog +import logging + +def configure_logging(config): + LOG_LEVEL = getattr(logging, config['logging']['level']) + LOG_TIME_FORMAT = config['logging']['format']['time'] + LOG_RENDERER = config['logging']['format']['renderer'] + structlog.configure( + wrapper_class=structlog.make_filtering_bound_logger(LOG_LEVEL), + processors=[ + structlog.processors.TimeStamper(fmt=LOG_TIME_FORMAT), + structlog.processors.add_log_level + ] + ) + if LOG_RENDERER == "console": + structlog.configure(processors=[*structlog.get_config()["processors"], structlog.dev.ConsoleRenderer()]) + else: + structlog.configure(processors=[*structlog.get_config()["processors"], structlog.processors.JSONRenderer()]) + +def getLogger(): + return structlog.get_logger() + \ No newline at end of file diff --git a/src/rookify/modules/analyze_ceph/main.py b/src/rookify/modules/analyze_ceph/main.py index f9042f5..cf70c66 100644 --- a/src/rookify/modules/analyze_ceph/main.py +++ b/src/rookify/modules/analyze_ceph/main.py @@ -3,11 +3,14 @@ import json from ..module import ModuleHandler +from rookify.logger import getLogger class AnalyzeCephHandler(ModuleHandler): def run(self) -> dict: + log = getLogger() + commands = [ 'mon dump', 'osd dump', @@ -34,5 +37,5 @@ def run(self) -> dict: results['ssh']['osd'][node] = { 'devices': devices } - + log.info("AnalyzeCephHandler ran successfully.") return results diff --git a/src/rookify/modules/migrate_monitors/main.py b/src/rookify/modules/migrate_monitors/main.py index 359c34c..c8cda57 100644 --- a/src/rookify/modules/migrate_monitors/main.py +++ b/src/rookify/modules/migrate_monitors/main.py @@ -1,6 +1,10 @@ # -*- coding: utf-8 -*- from ..module import ModuleHandler +from rookify.logger import getLogger class MigrateMonitorsHandler(ModuleHandler): - pass + def run(self): + log = getLogger() + log.info("MigrateMonitorsHandler ran successfully.") + diff --git a/src/rookify/modules/migrate_osds/main.py b/src/rookify/modules/migrate_osds/main.py index 141a23a..b631756 100644 --- a/src/rookify/modules/migrate_osds/main.py +++ b/src/rookify/modules/migrate_osds/main.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from ..module import ModuleHandler, ModuleException +from rookify.logger import getLogger class MigrateOSDsHandler(ModuleHandler):