I have a method called import_customers()
which loads csv-like data.
This methods logs to log-level INFO.
In one case I want to avoid this logging.
I see several ways:
Variant 1: a new kwarg like do_logging=True
which I can switch to false.
Variant 2: Use some magic context which ignores this line.
with IgnoreLoggingContext() as context:
import_customers()
How could I implement IgnoreLoggingContext()
?
If you think V1 is better, then please leave a comment.
It depends on your need. If you want to disable the whole logging, it would be the simplest:
from contextlib import contextmanager
import logging
@contextmanager
def IgnoreLoggingContext():
root = logging.getLogger()
origin_level = root.getEffectiveLevel()
root.setLevel(logging.WARNING) # Or whatever you want
yield
root.setLevel(origin_level)
with IgnoreLoggingContext():
do_something()
Or you can pass a variable to the manager to specify which logger to disable:
@contextmanager
def IgnoreLoggingContext(name):
logger = logging.getLogger(name)
origin_level = logger.getEffectiveLevel()
logger.setLevel(logging.WARNING) # Or whatever you want
yield
logger.setLevel(origin_level)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With