aurweb/aurweb/logging.py
Kevin Morris a06f4ec19c
fix(fastapi): centralize logging initialization
With this change, we provide a wrapper to `logging.getLogger`
in the `aurweb.logging` module. Modules wishing to log using
logging.conf should get their module-local loggers by calling
`aurweb.logging.getLogger(__name__)`, similar to `logging.getLogger`,
this way initialization with logging.conf is guaranteed.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-10-21 10:40:52 -07:00

21 lines
620 B
Python

import logging
import logging.config
import os
import aurweb.config
aurwebdir = aurweb.config.get("options", "aurwebdir")
config_path = os.path.join(aurwebdir, "logging.conf")
logging.config.fileConfig(config_path, disable_existing_loggers=False)
def get_logger(name: str) -> logging.Logger:
""" A logging.getLogger wrapper. Importing this function and
using it to get a module-local logger ensures that logging.conf
initialization is performed wherever loggers are used.
:param name: Logger name; typically `__name__`
:returns: name's logging.Logger
"""
return logging.getLogger(name)