Logger_config: should now expand ~ correctly in filepath
This commit is contained in:
@@ -36,8 +36,6 @@ def get_config_path(env: str = "dev") -> Path:
|
||||
|
||||
def setup_logger(name="aareDAQ", base_dir: str | None = '~/tmp/mxlogs', config_path: str | None = None):
|
||||
# switch to production mode using: $ APP_ENV=prod python main.py
|
||||
if name == "aareDAQ":
|
||||
base_dir="/tmp/mxlogs"
|
||||
env = os.getenv("APP_ENV", "dev") # default: dev
|
||||
|
||||
if config_path is None:
|
||||
@@ -48,25 +46,19 @@ def setup_logger(name="aareDAQ", base_dir: str | None = '~/tmp/mxlogs', config_p
|
||||
with open(config_file, "r") as f:
|
||||
config = yaml.safe_load(f.read())
|
||||
|
||||
env_log_dir = os.getenv("LOG_DIR")
|
||||
base_dir = base_dir or env_log_dir
|
||||
# Force base directory to be under the user's home dir
|
||||
effective_base = base_dir or '~/mxlogs'
|
||||
effective_base = os.path.abspath(os.path.expanduser(os.path.expandvars(effective_base)))
|
||||
|
||||
# Ensure directories for file handlers exist
|
||||
handlers = config.get("handlers", {})
|
||||
|
||||
for h in handlers.values():
|
||||
# RotatingFileHandler and FileHandler use "filename"
|
||||
|
||||
filename = h.get("filename")
|
||||
if not filename:
|
||||
continue
|
||||
if base_dir:
|
||||
abs_filename = os.path.join(base_dir, os.path.basename(filename))
|
||||
else:
|
||||
abs_filename = (
|
||||
filename
|
||||
if os.path.isabs(filename)
|
||||
else os.path.join(os.path.dirname(config_file), filename)
|
||||
)
|
||||
abs_filename = os.path.join(effective_base, os.path.basename(filename))
|
||||
|
||||
h["filename"] = abs_filename
|
||||
log_dir = os.path.dirname(abs_filename)
|
||||
|
||||
Reference in New Issue
Block a user