I have python like this:
def foo(): logger = logging.getLogger()
where expensive_func() is a function that returns a string and its expensive operation.
During development, the log level is set to DEBUG, and expensive_func() is executed, the message is logged, everything is in order.
The problem is that when I set the log level strictly more than DEBUG, say WARNING, in the production env, it is obvious that the returned value of expensive_func() will not be logged, but the expensive function itself will still be executed.
My question is: how to prevent python due to an expensive function when the logging level is WARNING?
I do not want to delete this debug line or add something like if level > DEBUG: return in an expensive function.
Thank.
EDIT
I visited the Lazy logring message string just now, but am not satisfied with this, mainly because:
- This is kind of ugly;
- Even if I wrap an expensive function with some Lazy class, what do I do when I have two expensive functions? (shown below).
class Lazy: def __init__(self, func, *a, **ka): self.func= func self.a = a self.ka= ka def __str__(self): return str(self.func(*self.a, **self.ka))
python logging lazy-evaluation
Not an ID Jan 27 '14 at 9:21 2014-01-27 09:21
source share