As Shane points out, using
log.debug("Some message: a=%sb=%s", a, b)
... instead of this:
log.debug("Some message: a=%sb=%s" % (a, b))
saves some time only by formatting the string if the message is actually logged.
This does not completely solve the problem, however, since you may have to preprocess the values ββfor formatting in a string, for example:
log.debug("Some message: a=%sb=%s", foo.get_a(), foo.get_b())
In this case, obj.get_a() and obj.get_b() will be computed even if no protocols occur.
The solution to this is to use lambda functions, but this requires several additional mechanisms:
class lazy_log_debug(object): def __init__(self, func): self.func = func logging.debug("%s", self) def __str__(self): return self.func()
... then you can log in with the following:
lazy_log_debug(lambda: "Some message: a=%sb=%s" % (foo.get_a(), foo.get_b()))
In this case, the lambda function will be called only if log.debug decides to perform formatting, so it calls the __str__ method.
Keep in mind: the overhead of this solution can greatly exceed the benefit :-) But, at least theoretically, this allows you to make complete lazy notes.