django logging日志优先级

原创博文 转载请注明出处!

参考官方文档:https://docs.djangoproject.com/en/2.1/topics/logging/

Loggers

A logger is the entry point into the logging system. Each logger is a named bucket to which messages can be written for processing.

A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:

  • DEBUG: Low level system information for debugging purposes
  • INFO: General system information
  • WARNING: Information describing a minor problem that has occurred.
  • ERROR: Information describing a major problem that has occurred.
  • CRITICAL: Information describing a critical problem that has occurred.

Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.

When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.

Once a logger has determined that a message needs to be processed, it is passed to a Handler.

向 logger 发出 log 时,会将 message 的日志级别与 logger 的日志级别进行比较。如果 log 的级别达到或超过 logger 本身的日志级别,则 log 将进行进一步处理。如果没有,则将忽略该消息。

一旦 logger 确定需要处理 log ,它就会传递给 Handlers 。

个人理解:logger 上设定的 level 是指此种 logger 处理的最低级别,所处理的 log 信息必须要达到这个级别以上

Handlers

The handler is the engine that determines what happens to each message in a logger. It describes a particular logging behavior, such as writing a message to the screen, to a file, or to a network socket.

Like loggers, handlers also have a log level. If the log level of a log record doesn’t meet or exceed the level of the handler, the handler will ignore the message.

A logger can have multiple handlers, and each handler can have a different log level. In this way, it is possible to provide different forms of notification depending on the importance of a message. For example, you could install one handler that forwards ERROR and CRITICAL messages to a paging service, while a second handler logs all messages (including ERRORand CRITICAL messages) to a file for later analysis.

handlers也具有日志级别。如果记录的 log 级别未达到超过 handler 的级别,则 handler 将忽略该消息。

 

综上理解:要记录的Log 需要先达到 logger 设定的 level,然后开始 handlers 处理,level 级别达到了的所有 handles 将对此 log 进行处理,否则忽略。

# demo 1:
# setting.py
    'handlers': {
        'default': {
            'level': 'INFO',                    # 'default'处理器规定处理的最低级别 低于这个级别不处理
            'class': 'logging.FileHandler',
            'filename': 'logging.log',          # log file name
            'formatter': 'simple'
        },
    },
    # 记录器
    'loggers': {
        'django': {
            'handlers': ['default'],
            'level': 'DEBUG',                   # 进入'django'这个记录器 的 log 需要的最低级别,低于这个级别不记录
            'propagate': False                  # propagate 向不向更高級別的 loggers 傳遞
        }
    },

# view.py
logger = logging.getLogger("django")
logger.debug('   index debug---------------\n') # 不记录 级别不够进入 handlers
logger.info('    index info---------------\n')  # 记录
logger.warning(' index warning-------------\n') # 记录

# -------------------------------------------------------------
# demo 2
# setting.py
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'logging.log',          # log file name
            'formatter': 'simple'
        },
    },
    # 记录器
    'loggers': {
        'django': {
            'handlers': ['default'],
            'level': 'INFO',
            'propagate': False                   # propagate 向不向更高級別的 loggers 傳遞
        }
    },
# view.py
logger = logging.getLogger("django")
logger.debug('   index debug---------------\n') # 不记录 loggers 级别不够
logger.info('    index info---------------\n')  # 记录
logger.warning(' index warning-------------\n') # 记录
个人test

 

 有不对的地方欢迎指出

posted @ 2018-12-12 09:16  Janey91  阅读(387)  评论(0编辑  收藏  举报