Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 24, 2024
1 parent a8780d1 commit 0aabad7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion c2cwsgiutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions c2cwsgiutils/loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from logging.config import fileConfig
from typing import Optional, cast

from plaster_pastedeploy import Loader as BaseLoader
Expand All @@ -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()

0 comments on commit 0aabad7

Please sign in to comment.