Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
src/legenddataflowscripts/_version.py

# New additions go at the top!
.vscode
*.c
.DS_Store
*.npz
Expand Down
42 changes: 38 additions & 4 deletions src/legenddataflowscripts/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def flush(self):


def build_log(
config_dict: dict, log_file: str | None = None, fallback: str = "prod"
config_dict: dict | str, log_file: str | None = None, fallback: str = "prod"
) -> logging.Logger:
"""Build a logger from a configuration dictionary.

Expand All @@ -39,18 +39,52 @@ def build_log(
log_file
The path to the log file.
"""
if isinstance(config_dict, str | dict):
# Accept either:
# - a str pointing to a logging properties file
# - a plain logging dict (handlers/formatters/etc.)
# - a dict already containing "options" -> {"logging": ...}
# If a dict is provided and it already contains an "options" key, assume
# caller set options explicitly (so we must not wrap it).
if isinstance(config_dict, str) or (
isinstance(config_dict, dict) and "options" not in config_dict
):
config_dict = {"options": {"logging": config_dict}}

if "logging" in config_dict["options"]:
if (
isinstance(config_dict, dict)
and "options" in config_dict
and "logging" in config_dict["options"]
):
log_config = config_dict["options"]["logging"]
# if it's a str, interpret it as a path to a file
if isinstance(log_config, str):
log_config = Props.read_from(log_config)

if log_file is not None:
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
log_config["handlers"]["dataflow"]["filename"] = log_file
# Ensure the logging config has a handlers->dataflow entry; create
# minimal structure if needed so we can set the filename.
if isinstance(log_config, dict):
handlers = log_config.setdefault("handlers", {})
dataflow = handlers.setdefault("dataflow", {})
# Set the filename for the dataflow handler
dataflow["filename"] = log_file
dataflow.setdefault("class", "logging.FileHandler")
dataflow.setdefault("level", "INFO")
log_config.setdefault("version", 1)
if (
"handlers" in log_config
and "dataflow" in log_config["handlers"]
and "root" not in log_config
and "loggers" not in log_config
):
dataflow_level = log_config["handlers"]["dataflow"].get(
"level", "INFO"
)
log_config["root"] = {
"level": dataflow_level,
"handlers": ["dataflow"],
}

dictConfig(log_config)
log = logging.getLogger(config_dict["options"].get("logger", "prod"))
Expand Down
Loading