From 651fca91f4ece71ff48b7984835065983c1f1e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Mon, 9 Dec 2024 17:03:55 +0100 Subject: [PATCH] Fix setup logging To avoid: ``` File "/usr/lib/python3.12/logging/__init__.py", line 1539, in info self._log(INFO, msg, args, **kwargs) File "/usr/lib/python3.12/logging/__init__.py", line 1684, in _log self.handle(record) File "/usr/lib/python3.12/logging/__init__.py", line 1700, in handle self.callHandlers(record) File "/venv/lib/python3.12/site-packages/sentry_sdk/integrations/logging.py", line 98, in sentry_patched_callhandlers return old_callhandlers(self, record) Message: '[%s] %r' Arguments: ('cached since 570.4s ago', {'status': 'ERROR', 'status_1': 'PENDING', 'created_at_1': datetime.datetime(2024, 12, 8, 16, 4, 28, 898425, tzinfo=datetime.timezone.utc)}) --- Logging error --- Traceback (most recent call last): File "/usr/lib/python3.12/logging/__init__.py", line 1163, in emit stream.write(msg + self.terminator) ^^^^^^^^^^^^ AttributeError: 'str' object has no attribute 'write' ``` --- c2cwsgiutils/loader.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/c2cwsgiutils/loader.py b/c2cwsgiutils/loader.py index 2d351420e..b84c0fb6a 100644 --- a/c2cwsgiutils/loader.py +++ b/c2cwsgiutils/loader.py @@ -3,7 +3,7 @@ from plaster_pastedeploy import Loader as BaseLoader -from c2cwsgiutils import get_config_defaults +from c2cwsgiutils import get_config_defaults, get_logconfig_dict _LOG = logging.getLogger(__name__) @@ -19,3 +19,22 @@ 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(): + logging.config.dictConfig(get_logconfig_dict(self.uri.path)) + else: + logging.basicConfig()