blivet icon indicating copy to clipboard operation
blivet copied to clipboard

Try harder to avoid AttributeErrors while logging

Open mulkieran opened this issue 9 years ago • 1 comments

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:

  1. use of a context manager that caught exception and logged a warning message around log calls.
  2. 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.

mulkieran avatar Apr 27 '15 15:04 mulkieran

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).

vpodzime avatar Apr 29 '15 06:04 vpodzime