Python logging
module provides log functions and classes for applications and libraries. There are different
levels of Logging messages (record) based on severity, include DEBUG, INFO, WARNING, ERROR, CRITICAL with incremental severity.
A logger can record a lot of logging messages. The logger also has severity levels as logging messages.
Messages that are lower than the logger in severity level will be ignored, otherwise they will be passed to a Handler.
The default logging level is WARNING.
The simplest, use logging module directly:
>>> import logging >>> logging.debug("log debug")#lower than default level, ignored >>> logging.info("log info")#lower than default level, ignored >>> logging.warning("log warning")
WARNING:root:log warning
>>> logging.error("log error")
ERROR:root:log errorLet's create a logger object:
>>> import logging >>> lgr = logging.getLogger('test') >>> lgr.debug("log debug")#lower than default level, ignored >>> lgr.info("log info")#lower than default level, ignored >>> lgr.warning("log warning")#lower than default level, ignored
log warning
>>> lgr.error("log error")
log error
>>> lgr.setLevel(logging.DEBUG) >>> lgr.debug("log debug")
log debugAdd handler to logger:
import logging, logging.config LOGGING = { 'version': 1 'disable_existing_loggers': True 'formatters': { 'default': { 'format': '%(levelname)s %(asctime)s %(message)s', 'datefmt': '%m/%d/%Y %H:%M%S' } }, 'handlers': { 'null': { 'level': 'DEBUG', 'class': 'logging.NullHandler' } 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'default' }, 'file': { 'level': 'INFO', 'class': 'logging.handlers.TimedRotatingFileHandler', 'formatter': 'default', 'filename': 'temp', 'when': 'M', 'interval': 1, } }, 'Loggers': { 'EndMemo': { 'level': 'INFO', 'handlers': ['console','file'], 'propagate': True } } } logging.config.dictConfig(LOGGING) lgr = logging.getLogger('EndMemo') lgr.info('log test')