Skip to content

Commit

Permalink
feat: adding logger.py to configure structlog and then implemented it…
Browse files Browse the repository at this point in the history
… in __main__.py and in the modules
  • Loading branch information
boekhorstb1 committed Mar 4, 2024
1 parent 02832d9 commit df70738
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/config.example.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/rookify/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
22 changes: 22 additions & 0 deletions src/rookify/logger.py
Original file line number Diff line number Diff line change
@@ -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()

5 changes: 4 additions & 1 deletion src/rookify/modules/analyze_ceph/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -34,5 +37,5 @@ def run(self) -> dict:
results['ssh']['osd'][node] = {
'devices': devices
}

log.info("AnalyzeCephHandler ran successfully.")
return results
6 changes: 5 additions & 1 deletion src/rookify/modules/migrate_monitors/main.py
Original file line number Diff line number Diff line change
@@ -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.")

1 change: 1 addition & 0 deletions src/rookify/modules/migrate_osds/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler, ModuleException
from rookify.logger import getLogger

class MigrateOSDsHandler(ModuleHandler):

Expand Down

0 comments on commit df70738

Please sign in to comment.