If you want to change the log level for one iteration of a loop or one call of a function, it can be done by calling setLevel
on the current logger instance.
import logging
"This statement will be shown")
logging.error(
"This will not be shown")
logging.debug(
"DEBUG")
logging.getLogger().setLevel(
"This debug statement will be shown")
logging.debug(
"ERROR")
logging.getLogger().setLevel(
"This debug statement will be hidden") logging.debug(
This results in
ERROR:root:This statement will be shown
DEBUG:root:This debug statement will be shown