Skip to content

Commit

Permalink
Move logger initialization to main and keep Single Responsibility Pri…
Browse files Browse the repository at this point in the history
…nciple

Signed-off-by: Tobias Wolf <[email protected]>
  • Loading branch information
NotTheEvilOne committed Apr 28, 2024
1 parent 7d2283d commit 150acf3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
19 changes: 11 additions & 8 deletions src/rookify/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from types import MappingProxyType
from .yaml import load_yaml, save_yaml
from rookify.logger import configure_logging, getLogger
from rookify.logger import configure_logging, get_logger


def main() -> None:
Expand All @@ -14,19 +14,22 @@ def main() -> None:
config = load_yaml("config.yaml")
except FileNotFoundError as err:
raise SystemExit(f"Could not load config: {err}")
preflight_modules, migration_modules = rookify.modules.load_modules(
config["migration_modules"]
)

# Configure structlog logging
# Configure logging
try:
configure_logging(config)
log = getLogger()
log.info("Structlog configured successfully.")
configure_logging(config["logging"])
except Exception as e:
raise SystemExit(f"Error configuring logging: {e}")

log = get_logger()
log.debug("Executing Rookify")

preflight_modules, migration_modules = rookify.modules.load_modules(
config["migration_modules"]
)

module_data = dict()

try:
module_data.update(load_yaml(config["general"]["module_data_file"]))
except FileNotFoundError:
Expand Down
10 changes: 6 additions & 4 deletions src/rookify/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@


def configure_logging(config: Dict[str, Any]) -> None:
LOG_LEVEL = getattr(logging, config["logging"]["level"])
LOG_TIME_FORMAT = config["logging"]["format"]["time"]
LOG_RENDERER = config["logging"]["format"]["renderer"]
LOG_LEVEL = getattr(logging, config["level"], logging.INFO)
LOG_TIME_FORMAT = config["format"]["time"]
LOG_RENDERER = config["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=[
Expand All @@ -31,5 +33,5 @@ def configure_logging(config: Dict[str, Any]) -> None:
)


def getLogger() -> structlog.getLogger:
def get_logger() -> structlog.getLogger:
return structlog.get_logger()
25 changes: 6 additions & 19 deletions src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import fabric
import jinja2
import structlog
from rookify.logger import configure_logging
from rookify.logger import get_logger
from typing import Any, Dict, List, Optional


Expand All @@ -22,17 +22,6 @@ class ModuleHandler:
ModuleHandler is an abstract class that modules have to extend.
"""

class __Logger:
def __init__(self, config: Dict[str, Any]):
try:
configure_logging(config)
except ImportError as e:
raise ModuleException(f"Error initializing logger: {e}")

@property
def getLogger(self) -> structlog.getLogger:
return structlog.getLogger

class __Ceph:
def __init__(self, config: Dict[str, Any]):
try:
Expand Down Expand Up @@ -175,7 +164,7 @@ def __init__(self, config: Dict[str, Any], data: Dict[str, Any], module_path: st
self.__ceph: Optional[ModuleHandler.__Ceph] = None
self.__k8s: Optional[ModuleHandler.__K8s] = None
self.__ssh: Optional[ModuleHandler.__SSH] = None
self.__logger: Optional[structlog.getLogger] = None
self.__logger = get_logger()

@abc.abstractmethod
def preflight(self) -> None:
Expand All @@ -199,6 +188,10 @@ def ceph(self) -> __Ceph:
self.__ceph = ModuleHandler.__Ceph(self._config["ceph"])
return self.__ceph

@property
def logger(self) -> structlog.getLogger:
return self.__logger

@property
def k8s(self) -> __K8s:
if self.__k8s is None:
Expand All @@ -211,12 +204,6 @@ def ssh(self) -> __SSH:
self.__ssh = ModuleHandler.__SSH(self._config["ssh"])
return self.__ssh

@property
def logger(self) -> structlog.getLogger:
if self.__logger is None:
self.__logger = ModuleHandler.__Logger(self._config)
return self.__logger.getLogger()

def load_template(self, filename: str, **variables: Any) -> __Template:
template_path = os.path.join(self.__module_path, "templates", filename)
template = ModuleHandler.__Template(template_path)
Expand Down

0 comments on commit 150acf3

Please sign in to comment.