Skip to content

Commit

Permalink
Adjust showwarning, not formatwarning.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Dec 18, 2024
1 parent 3a7437a commit a589f26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 5 additions & 9 deletions ops/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions testing/src/scenario/_ops_main_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a589f26

Please sign in to comment.