diff --git a/c2cwsgiutils/__init__.py b/c2cwsgiutils/__init__.py index b450acd9..b4d94ff9 100644 --- a/c2cwsgiutils/__init__.py +++ b/c2cwsgiutils/__init__.py @@ -49,7 +49,8 @@ def _create_handlers(config: configparser.ConfigParser) -> dict[str, Any]: if "filters" in block: conf["filters"] = block["filters"] if "kwargs" in block: - conf["kwargs"] = block["kwargs"] + kwargs = ast.literal_eval(block["kwargs"]) + conf.update(kwargs) d_handlers[hh] = conf return d_handlers diff --git a/c2cwsgiutils/loader.py b/c2cwsgiutils/loader.py index 2d351420..f3bfec16 100644 --- a/c2cwsgiutils/loader.py +++ b/c2cwsgiutils/loader.py @@ -1,4 +1,5 @@ import logging +from logging.config import fileConfig from typing import Optional, cast from plaster_pastedeploy import Loader as BaseLoader @@ -19,3 +20,32 @@ def _get_defaults(self, defaults: Optional[dict[str, str]] = None) -> dict[str, def __repr__(self) -> str: """Get the object representation.""" return f'c2cwsgiutils.loader.Loader(uri="{self.uri}")' + + def setup_logging(self, defaults=None): + """ + Set up logging via :func:`logging.config.fileConfig`. + + Defaults are specified for the special ``__file__`` and ``here`` + variables, similar to PasteDeploy config loading. Extra defaults can + optionally be specified as a dict in ``defaults``. + + :param defaults: The defaults that will be used when passed to + :func:`logging.config.fileConfig`. + :return: ``None``. + + """ + if "loggers" in self.get_sections(): + parser = self._get_parser(defaults) + + handlers = [k.strip() for k in parser.items("handlers")["keys"].split(",")] + for hh in handlers: + block = parser.items(f"handler_{hh}") + if "kwargs" in block: + block["kwargs"] = block["kwargs"].replace("ext://", "") + + defaults = self._get_defaults(defaults) + with tempfile.NamedTemporaryFile() as temp_file: + parser.write(temp_file) + fileConfig(temp_file.name, defaults, disable_existing_loggers=False) + else: + logging.basicConfig()