Django Log

Logging

Python logging 主要有以下四部分组成:

  1. Loggers
  2. Handlers
  3. Filters
  4. Formatters

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.

log leve: DEBUG -> INFO -> WARNING -> ERROR -> CRITICAL

  • 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.

message log’level <= logger’level 消息才会被logger接收,logger会把消息传递给Handler进行处理


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.

同Logger一样,Handler也会检查message log’level,Logger可以有多个Handler,例如,


Filters

A filter is used to provide additional control over which log records are passed from logger to handler.

Filters can be installed on loggers or on handlers; multiple filters can be used in a chain to perform multiple filtering actions.


Formatters

Formatters describe the exact format of that text.

logrecord-attributes:https://docs.python.org/3/library/logging.html#logrecord-attributes

Note

disable_existing_loggers

If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the default) then all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger. Thus you should be very careful using ‘disable_existing_loggers’: True; it’s probably not what you want. Instead, you can set disable_existing_loggers to False and redefine some or all of the default loggers; or you can set LOGGING_CONFIG to None and handle logging config yourself.