blivet
blivet copied to clipboard
Try harder to avoid AttributeErrors while logging
We log enthusiastically in a lot of our methods and so we can end up with AttributeErrors whenever an object is incomplete for one reason or another. Object incompleteness can arise for many reasons. AttributeErrors in necessary code can not always be eliminated, but it's wrong to crash in logging code.
Log methods could be protected from crashes by:
- use of a context manager that caught exception and logged a warning message around log calls.
- writing own log.info, log.debug, etc. wrapper methods and making lazy calls in the log methods, like:
log.warning("a helpful string %d", lambda : self.size)
and each wrapper method is:
def debug(fmt_str, *args, **kwargs):
""" wrapper method for log.debug"""
evaluated_args = []
for f in args:
try:
evaluated_args.append(f())
except Exception:
log.warning("Failed to log message associate with format string %s, argument at index %d could not be evaluated", fmt_str, len(evaluated_args))
return
log.debug(fmt_str, *evaluated_args, **kwargs))
Note that the above is an incomplete solution and the whole idea is probably harder than it looks.
It is really impossible to avoid all the AttributeErrors in logging? I'd prefer if focused on eliminating those instead of spending hours on hiding them. If not, we would probably have to do something like the proposed solution above, just with a best-effort principle -- instead of returning on exception, log what's possible to log (that would require some complex and complicated changes, though).