From 42d25e17589f0fae235cf73bec38100cc876deb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Sun, 24 Nov 2024 21:58:00 +0100 Subject: [PATCH] f --- c2cwsgiutils/__init__.py | 4 +++- c2cwsgiutils/loader.py | 32 ++++++++++++++++++++++++++++++++ c2cwsgiutils/setup_process.py | 8 -------- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/c2cwsgiutils/__init__.py b/c2cwsgiutils/__init__.py index b450acd91..70a7fff0d 100644 --- a/c2cwsgiutils/__init__.py +++ b/c2cwsgiutils/__init__.py @@ -1,3 +1,4 @@ +import ast import configparser import logging import os @@ -49,7 +50,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 2d351420e..9d84b5577 100644 --- a/c2cwsgiutils/loader.py +++ b/c2cwsgiutils/loader.py @@ -1,4 +1,6 @@ import logging +import tempfile +from logging.config import fileConfig from typing import Optional, cast from plaster_pastedeploy import Loader as BaseLoader @@ -19,3 +21,33 @@ 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: Optional[dict[str, str]] = None) -> 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``. + + Arguments: + defaults: The defaults that will be used when passed to + :func:`logging.config.fileConfig`. + + """ + if "loggers" in self.get_sections(): + parser = self._get_parser(defaults) + + handlers = [k.strip() for k in parser["handlers"]["keys"].split(",")] + for hh in handlers: + block = parser[f"handler_{hh}"] + if "kwargs" in block: + block["kwargs"] = block["kwargs"].replace("'ext://sys.stdout'", "sys.stdout") + + defaults = self._get_defaults(defaults) + with tempfile.NamedTemporaryFile("w+t", encoding="utf-8") as temp_file: + parser.write(temp_file) + temp_file.flush() + fileConfig(temp_file.name, defaults, disable_existing_loggers=False) + else: + logging.basicConfig() diff --git a/c2cwsgiutils/setup_process.py b/c2cwsgiutils/setup_process.py index 72a27c880..f85e9a98f 100644 --- a/c2cwsgiutils/setup_process.py +++ b/c2cwsgiutils/setup_process.py @@ -5,7 +5,6 @@ """ import argparse -import ast import logging import warnings from typing import Any, Callable, Optional, TypedDict, cast @@ -99,13 +98,6 @@ def bootstrap_application( loader = get_config_loader(config_uri) print(dir(loader)) - handlers = [k.strip() for k in loader.get_settings()["handlers"]["keys"].split(",")] - for hh in handlers: - block = loader.get_settings()[f"handler_{hh}"] - if "kwargs" in block: - kwargs = ast.literal_eval(block["kwargs"]) - block["kwargs"] = kwargs - loader.setup_logging(options) logging.getLogger(__name__).info("Loading the application from %s", config_uri) return cast(PyramidEnv, bootstrap(config_uri, options=options))