diff --git a/ops/log.py b/ops/log.py index f1a5984e6..fbdbb12b1 100644 --- a/ops/log.py +++ b/ops/log.py @@ -23,11 +23,6 @@ from ops.model import _ModelBackend -# We do this on module import because some warnings are issued before we set up -# the framework, and we need to capture those as well. -logging.captureWarnings(True) - - class JujuLogHandler(logging.Handler): """A handler for sending logs and warnings to Juju via juju-log.""" @@ -65,18 +60,19 @@ def setup_root_logging( logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(JujuLogHandler(model_backend)) + logging.captureWarnings(True) - def custom_warning_formatter( + def custom_showwarning( message: typing.Union[str, Warning], category: typing.Type[Warning], filename: str, lineno: int, - _: typing.Optional[str] = None, + *_: typing.Any, ) -> str: - """Like the default formatter, but don't include the code.""" + """Like the default showwarning, but don't include the code.""" return f'{filename}:{lineno}: {category.__name__}: {message}' - warnings.formatwarning = custom_warning_formatter + warnings.showwarning = custom_showwarning if debug: handler = logging.StreamHandler() diff --git a/testing/src/scenario/_ops_main_mock.py b/testing/src/scenario/_ops_main_mock.py index 12894f26a..7f8d99750 100644 --- a/testing/src/scenario/_ops_main_mock.py +++ b/testing/src/scenario/_ops_main_mock.py @@ -7,6 +7,7 @@ import marshal import re import sys +import warnings from typing import TYPE_CHECKING, Any, Dict, FrozenSet, List, Sequence, Set import ops @@ -137,12 +138,17 @@ def _load_charm_meta(self): return ops.CharmMeta.from_yaml(metadata, actions_metadata) def _setup_root_logging(self): - # Ops sets sys.excepthook to go to Juju's debug-log, but that's not - # useful in a testing context, so we reset it here. + # The warnings module captures this in _showwarning_orig, but we + # shouldn't really be using a private method, so capture it ourselves as + # well. + original_showwarning = warnings.showwarning super()._setup_root_logging() # Ops also sets up logging to capture warnings, but we want the normal # output. logging.captureWarnings(False) + warnings.showwarning = original_showwarning + # Ops sets sys.excepthook to go to Juju's debug-log, but that's not + # useful in a testing context, so we reset it here. sys.excepthook = sys.__excepthook__ def _make_storage(self, _: _Dispatcher):