Skip to content

Commit

Permalink
Split up logging configuration method
Browse files Browse the repository at this point in the history
#3

The `configure_logging()` method did multiple things: find and import a
logging configuration module, load the logging configuration dictionary,
and apply the dictionary configuration to the Python logger.

This commit will refactor `configure_logging()` into two methods.

1. `find_and_load_logging_conf()` will return a dictionary configuration
2. `configure_logging()` will apply the dictionary configuration

The return value of `configure_logging()` will remain the same, so there
will not be any changes to the programming API.

Also, now that the `configure_logging()` method is in the same module as
the logging configuration dictionary, that dictionary will be used by
default, instead of finding the module separately.
  • Loading branch information
br3ndonland committed Apr 18, 2021
1 parent 0ba94fc commit 373c98d
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions inboard/logging_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,42 @@
import os
import sys
from pathlib import Path
from typing import Optional


def find_and_load_logging_conf(logging_conf: str) -> dict:
"""Find and load a logging configuration module or file."""
logging_conf_path = Path(logging_conf)
spec = (
importlib.util.spec_from_file_location("confspec", logging_conf_path)
if logging_conf_path.is_file() and logging_conf_path.suffix == ".py"
else importlib.util.find_spec(logging_conf)
)
if not spec:
raise ImportError(f"Unable to import {logging_conf_path}")
logging_conf_module = importlib.util.module_from_spec(spec)
exec_module = getattr(spec.loader, "exec_module")
exec_module(logging_conf_module)
if not hasattr(logging_conf_module, "LOGGING_CONFIG"):
raise AttributeError(f"No LOGGING_CONFIG in {logging_conf_module.__name__}")
logging_conf_dict = getattr(logging_conf_module, "LOGGING_CONFIG")
if not isinstance(logging_conf_dict, dict):
raise TypeError("LOGGING_CONFIG is not a dictionary instance")
return logging_conf_dict


def configure_logging(
logger: logging.Logger = logging.getLogger(),
logging_conf: str = os.getenv("LOGGING_CONF", "inboard.logging_conf"),
logging_conf: Optional[str] = os.getenv("LOGGING_CONF"),
) -> dict:
"""Configure Python logging based on a path to a logging module or file."""
"""Configure Python logging given the name of a logging module or file."""
try:
logging_conf_path = Path(logging_conf)
spec = (
importlib.util.spec_from_file_location("confspec", logging_conf_path)
if logging_conf_path.is_file() and logging_conf_path.suffix == ".py"
else importlib.util.find_spec(logging_conf)
)
if not spec:
raise ImportError(f"Unable to import {logging_conf}")
logging_conf_module = importlib.util.module_from_spec(spec)
exec_module = getattr(spec.loader, "exec_module")
exec_module(logging_conf_module)
if not hasattr(logging_conf_module, "LOGGING_CONFIG"):
raise AttributeError(f"No LOGGING_CONFIG in {logging_conf_module.__name__}")
logging_conf_dict = getattr(logging_conf_module, "LOGGING_CONFIG")
if not isinstance(logging_conf_dict, dict):
raise TypeError("LOGGING_CONFIG is not a dictionary instance")
if not logging_conf:
logging_conf_path = __name__
logging_conf_dict = LOGGING_CONFIG
else:
logging_conf_path = logging_conf
logging_conf_dict = find_and_load_logging_conf(logging_conf_path)
logging.config.dictConfig(logging_conf_dict)
logger.debug(f"Logging dict config loaded from {logging_conf_path}.")
return logging_conf_dict
Expand Down

0 comments on commit 373c98d

Please sign in to comment.