Skip to content

Commit

Permalink
util: add log_level kwarg for logexc() (#5125)
Browse files Browse the repository at this point in the history
Many failures are being treated as warnings instead of errors due
to usage of logexc() to emit the failure.

Add log_level parameter to allow increasing the log level without
requiring an additional log.

Add tests but I'm unsure why its not logging the backtrace when
the failure occurs within the test method.

Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
  • Loading branch information
cjp256 committed Apr 10, 2024
1 parent 5c200af commit 93f5a01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
12 changes: 4 additions & 8 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,21 +1797,17 @@ def get_config_logfiles(cfg):
return list(set(logs + rotated_logs))


def logexc(log, msg, *args):
# Setting this here allows this to change
# levels easily (not always error level)
# or even desirable to have that much junk
# coming out to a non-debug stream
if msg:
log.warning(msg, *args)
def logexc(log, msg, *args, log_level: int = logging.WARNING) -> None:
log.log(log_level, msg, *args)

# Debug gets the full trace. However, nose has a bug whereby its
# logcapture plugin doesn't properly handle the case where there is no
# actual exception. To avoid tracebacks during the test suite then, we'll
# do the actual exc_info extraction here, and if there is no exception in
# flight, we'll just pass in None.
exc_info = sys.exc_info()
if exc_info == (None, None, None):
exc_info = None
exc_info = None # type: ignore
log.debug(msg, exc_info=exc_info, *args)


Expand Down
36 changes: 36 additions & 0 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3245,3 +3245,39 @@ def test_file_present(self, content, caplog, tmpdir):
assert {"scopes": ["network"]} == util.read_hotplug_enabled_file(
MockPath(target_file.strpath)
)


class TestLogExc:
def test_logexc(self, caplog):
try:
_ = 1 / 0
except Exception as _:
util.logexc(LOG, "an error occurred")

assert caplog.record_tuples == [
(
"tests.unittests.test_util",
logging.WARNING,
"an error occurred",
),
("tests.unittests.test_util", logging.DEBUG, "an error occurred"),
]

@pytest.mark.parametrize(
"log_level",
[logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR],
)
def test_logexc_with_log_level(self, caplog, log_level):
try:
_ = 1 / 0
except Exception as _:
util.logexc(LOG, "an error occurred", log_level=log_level)

assert caplog.record_tuples == [
(
"tests.unittests.test_util",
log_level,
"an error occurred",
),
("tests.unittests.test_util", logging.DEBUG, "an error occurred"),
]

0 comments on commit 93f5a01

Please sign in to comment.