1

The Python's pprint function I use for logging dictionaries is quite slow for larger objects. I cannot find a way to completely turn off the processing of that function using the standard logging library. Run the code below as an example:

logging.disable(50)
logging.log(msg=sum(range(20000000)), level=0)

Even though the result did not show up, the sum was still definitely computed (crank up the number inside of range to see what I mean). Is there a standard way in the logging module to disable the computing completely? If not, other suggestions are also welcomed.

1
  • 4
    The sum will always be computed in this case, it gets computed before it is passed to the function Commented Jun 24, 2018 at 17:32

1 Answer 1

1

the problem is is that anything that is at the root level (this includes arguments into a function, just not the interior of the function,) is computed as soon as python see's it(ie its computed before python ever calls the log function)... to solve this you can put your logic inside a function ... and then only execute the logic if it is appropriate

def lazy_log(callable_fn,log_name,log_level):
    logger = logging.getLogger(log_name)
    if logger.getEffectiveLevel() > log_level:
        return
    logger.log(msg=str(callable_fn()),level=log_level)

I guess might work for you

lazy_log(lambda : sum(range(1000000)), None, 51)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.