Skip to content

Commit 2f2cce8

Browse files
authored
log: fix logging when log file is None (#30)
2 parents ec333b0 + f95a8f2 commit 2f2cce8

1 file changed

Lines changed: 27 additions & 28 deletions

File tree

  • src/legenddataflowscripts/utils

src/legenddataflowscripts/utils/log.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def build_log(
3939
log_file
4040
The path to the log file.
4141
"""
42+
if log_file is None:
43+
return logging.getLogger(fallback)
44+
4245
# Accept either:
4346
# - a str pointing to a logging properties file
4447
# - a plain logging dict (handlers/formatters/etc.)
@@ -60,39 +63,35 @@ def build_log(
6063
if isinstance(log_config, str):
6164
log_config = Props.read_from(log_config)
6265

63-
if log_file is not None:
64-
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
65-
# Ensure the logging config has a handlers->dataflow entry; create
66-
# minimal structure if needed so we can set the filename.
67-
if isinstance(log_config, dict):
68-
handlers = log_config.setdefault("handlers", {})
69-
dataflow = handlers.setdefault("dataflow", {})
70-
# Set the filename for the dataflow handler
71-
dataflow["filename"] = log_file
72-
dataflow.setdefault("class", "logging.FileHandler")
73-
dataflow.setdefault("level", "INFO")
74-
log_config.setdefault("version", 1)
75-
if (
76-
"handlers" in log_config
77-
and "dataflow" in log_config["handlers"]
78-
and "root" not in log_config
79-
and "loggers" not in log_config
80-
):
81-
dataflow_level = log_config["handlers"]["dataflow"].get(
82-
"level", "INFO"
83-
)
84-
log_config["root"] = {
85-
"level": dataflow_level,
86-
"handlers": ["dataflow"],
87-
}
66+
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
67+
# Ensure the logging config has a handlers->dataflow entry; create
68+
# minimal structure if needed so we can set the filename.
69+
if isinstance(log_config, dict):
70+
handlers = log_config.setdefault("handlers", {})
71+
dataflow = handlers.setdefault("dataflow", {})
72+
# Set the filename for the dataflow handler
73+
dataflow["filename"] = log_file
74+
dataflow.setdefault("class", "logging.FileHandler")
75+
dataflow.setdefault("level", "INFO")
76+
log_config.setdefault("version", 1)
77+
if (
78+
"handlers" in log_config
79+
and "dataflow" in log_config["handlers"]
80+
and "root" not in log_config
81+
and "loggers" not in log_config
82+
):
83+
dataflow_level = log_config["handlers"]["dataflow"].get("level", "INFO")
84+
log_config["root"] = {
85+
"level": dataflow_level,
86+
"handlers": ["dataflow"],
87+
}
8888

8989
dictConfig(log_config)
9090
log = logging.getLogger(config_dict["options"].get("logger", "prod"))
9191

9292
else:
93-
if log_file is not None:
94-
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
95-
logging.basicConfig(level=logging.INFO, filename=log_file, filemode="w")
93+
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
94+
logging.basicConfig(level=logging.INFO, filename=log_file, filemode="w")
9695

9796
log = logging.getLogger(fallback)
9897

0 commit comments

Comments
 (0)