Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 25, 2024
1 parent a8780d1 commit 42d25e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion c2cwsgiutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import configparser
import logging
import os
Expand Down Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions c2cwsgiutils/loader.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
8 changes: 0 additions & 8 deletions c2cwsgiutils/setup_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import argparse
import ast
import logging
import warnings
from typing import Any, Callable, Optional, TypedDict, cast
Expand Down Expand Up @@ -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))

0 comments on commit 42d25e1

Please sign in to comment.