在我的代码中,我使用logging.info(...
而在此之前我使用logging.basicConfig(filename=...
配置。是否可以在代码中保留日志行而不做任何事情?
You can use:
logging.disable(logging.CRITICAL)
to disable all logging calls which are at level CRITICAL or below. Effectively this disables all logging calls.
You can enable the logging for all loggers again (at their own logging levels) by doing:
logging.disable(logging.NOTSET)
EDIT: it seems that disabled
is not supposed to be meant for public use. Look at Maggyero's answer for alternative solutions.
Just disable the log handler and it won't write to anything anymore.
logging.getLogger().disabled = True
Do note that every logger can have handlers so there might be more.
Logging has the following structure :
logging.WARNING
by default for the root logger and logging.NOTSET
by default for non-root loggers) and an effective level (the effective level of the parent logger for non-root loggers with a level logging.NOTSET
and the level of the logger otherwise);logging.NOTSET
by default);Logging has the following process (represented by a flowchart):
Therefore to disable a particular logger you can do one of the following:
Set the level of the logger to logging.CRITICAL + 1
.
Using the main API:
import logging logger = logging.getLogger("foo") logger.setLevel(logging.CRITICAL + 1)
Using the config API:
import logging.config logging.config.dictConfig({ "version": 1, "loggers": { "foo": { "level": logging.CRITICAL + 1 } } })
Add a filter lambda record: False
to the logger.
Using the main API:
import logging logger = logging.getLogger("foo") logger.addFilter(lambda record: False)
Using the config API:
import logging.config logging.config.dictConfig({ "version": 1, "filters": { "all": { "()": lambda: (lambda record: False) } }, "loggers": { "foo": { "filters": ["all"] } } })
Remove the existing handlers of the logger, add a handler logging.NullHandler()
to the logger (to prevent events from being handled by the handler logging.lastResort
which is a logging.StreamHandler
using the current stream sys.stderr
and a level logging.WARNING
) and set the attribute propagate
of the logger to False
(to prevent events from being handled by the handlers of the ancestor loggers of the logger).
Using the main API:
import logging logger = logging.getLogger("foo") for handler in logger.handlers.copy(): logger.removeHandler(handler) logger.addHandler(logging.NullHandler()) logger.propagate = False
Using the config API:
import logging.config logging.config.dictConfig({ "version": 1, "handlers": { "null": { "class": "logging.NullHandler" } }, "loggers": { "foo": { "handlers": ["null"], "propagate": False } } })
Warning. — Contrary to approaches 1 and 2 which only prevent events logged by the logger from being emitted by the handlers of the logger and its ancestor loggers, approach 3 also prevents events logged by the descendant loggers of the logger (eg logging.getLogger("foo.bar")
) to be emitted by the handlers of the logger and its ancestor loggers.
Note. — Setting the attribute disabled
of the logger to True
is not yet another approach, as it is not part of the public API. See https://bugs.python.org/issue36318 :
import logging
logger = logging.getLogger("foo")
logger.disabled = True # DO NOT DO THIS
If you want full disable logs first should in logging.basicConfig() set levevl equal logging.NOTSET because is zero level and then logging.getLogger().disabled set True or False . https://docs.python.org/2/library/logging.html
Example for full disable:
import logging
if __name__ == '__main__':
logging.disable(logging.NOTSET)
logging.basicConfig(
format="%(levelname) -10s %(asctime)s %(filename)s:%(lineno)s %(message)s",
level=logging.NOTSET)
logging.getLogger().disabled = True # True, False
logging.critical("Critical")
logging.error("Error")
logging.warning("Warning")
logging.info("Info")
logging.debug("Debug")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.