This is the 9th day of my participation in Gwen Challenge
Django prints logs using python’s built-in logging module. Python’s logging configuration consists of four parts:
- Logger: Logger
- Handler: Handler
- Filter: Filter
- Format: Formatter
Recorder – Logger
- Loggers are the entry points to the logging system. Each logger is named bucket, and a message can be written to this bucket.
- Each logger has a log level. The log level indicates the severity of messages to be processed by the logger. The log levels are as follows:
DEBUG: low-level system information INFO: common system information WARNING: a minor fault occurs ERROR: a major fault occurs CRITICAL: a fatal fault occursCopy the code
- Each message written to logger is a log, and each log has a log level, which indicates the severity of the corresponding message. Each log record contains meta information that describes the event being printed.
- When a message is delivered to Logger, the log level of the message is compared to logger’s log level. If the log level of a message is greater than or equal to the logger log level, the message will continue to be processed. If less than, the message is ignored.
- Once Logger determines that a message needs to be processed, it passes the message to a Handler.
The Logger configuration
The logger value is a dictionary, and each key is a logger name, and each value is a dictionary that describes how to configure the corresponding logger instance.
- Level (optional), logger level.
- Propagate (optional), propagation Settings for Logger.
- Flters (optional), a list of identifiers for logger flters.
- Handlers (optional), a list of identifiers for handlers for Logger.
LOGGING = {
'loggers': {
'hahaha': {
'handlers': ['file_handler', 'console_handler'], 'level': 'DEBUG',
},
},
}
Copy the code
Handler -Handler
- Handler decides how to process each message in logger. It represents a specific logging behavior, such as writing messages to the screen, to a file, or to a network socket.
- Like Loggers, handlers have a log level. If the log level of a message is less than that of handler, the handler ignores the message.
- A Logger can have multiple handlers, and each handler can have a different log level. In this way, different forms of processing can be provided depending on the importance of the message.
LOGGING = {
'handlers': {
'hahaha': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
}
}
}
Copy the code
Filters – Filters
- Filter is used to provide additional control over the logging that is passed from logger to handler.
- By default, any message that meets the log level will be processed. By installing a FLter, you can add additional conditions to log handling. For example, you can install a FLter that only allows handling of ERROR messages from specific sources.
- Filters can also be used to change the priority of the logging to be processed. For example, if logging meets certain criteria, you can write a FLter to reduce logging from ERROR to WARNING.
- Filters can be installed on a Logger or handler; Multiple FLTERS can be connected in series to achieve multi-layer FLTER behavior.
Formatting – Formatters
Finally, logging needs to be converted to text. Formatter indicates the format of the text. Fomatter usually consists of python-formatted strings containing logging properties; You can also write custom Fomatter to implement your own format.
LOGGING = {
'formatters': {
'hahaha':{
'format': '%(asctime)s - %(pathname)s:%(lineno)d[%(levelname)s] - %(message)s'
},
'simple': {
'format': '%(asctime)s %(levelname)s %(message)s'
},
},
}
Copy the code
Format Log message Format
Django built-in logger
- Django gets all the logs
- Django. request handles request-related logs, with 5xx reporting an error log and 4xx reporting a WARNING log
- Django.db. backends handles logging of interactions with the database
- Django.security.* Handles security-related logs
- Django. Db. Backends. Schemea when handling database migration log
The related configuration
- setting.py
LOGGING = {'version': 1, 'disabLE_EXISTing_loGGERS ': False,# If this option is enabled, some logging is disabled. It is not recommended to set it to True 'formatters': {'verbose': {'format': '% (levelname) s (asctime) % s % (the message) s' # log format},' simple ': {' format' : '% (levelname) s % (the message) s'},},' filters' : {' require_debug_true: {' () ':' the django. Utils. The RequireDebugTrue ', # filter, only take effect when setting the DEBUG = True},}, 'handlers' : { 'console': { 'level': 'DEBUG', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', 'formatter': 'verbose'}, 'file': {# key config 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/data/logs/debug.log',# log file 'formatter': 'verbose', 'loggers': {' Django ': { ['file'], 'level': 'DEBUG', 'propagate': True, } }, }Copy the code
- Project urls. Py
from django.urls import path, include
urlpatterns = [
path('ops/', include("ops.urls")),
]
Copy the code
- app urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path('index/', views.index, name='index'),
]
Copy the code
- app views.py
from django.http import HttpResponse import logging logger = logging.getLogger('django') def index(request): logger.info('haha') return HttpResponse("Haha,this is index test!" )Copy the code