下面是关于django日志默认打印request请求信息的完整攻略:
1. 什么是django日志?
在Web应用程序开发中,我们需要实时地监测应用程序的运行状态,这就是日志的作用。Django提供了一套完善的日志系统,可以记录应用程序的运行状态,以帮助我们排查错误。
2. django日志的打印级别
Django提供了五种不同的打印级别,从低到高分别为:
- DEBUG:详细的日志记录,适用于开发和调试过程;
- INFO:描述信息,适用于生产环境中的较为重要的事件;
- WARNING:警告信息,表示当前情况可能导致错误;
- ERROR:错误信息,表示当前情况已经出错;
- CRITICAL:严重错误信息,表示程序已经无法继续执行。
3. 打印request请求信息的方法
除了Django默认的五个打印级别,我们还可以通过在logger配置中加入'request'
,来打印request请求信息。
我们可以在setting.py文件中进行如下的配置:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.request': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
这样配置之后,我们就可以在控制台或者日志文件中打印request请求信息了。
例如,我们在views.py文件中编写如下函数:
def index(request):
logger.debug('This is a debug message with request')
logger.info('This is an info message with request')
logger.warning('This is a warning message with request')
logger.error('This is an error message with request')
logger.critical('This is a critical message with request')
return HttpResponse("Hello, world.")
在访问http://127.0.0.1:8000/
的时候,我们会得到如下的输出:
[02/Dec/2021 16:25:21] "GET / HTTP/1.1" 200 13
This is a debug message with request
This is an info message with request
This is a warning message with request
This is an error message with request
This is a critical message with request
同时,我们也可以在控制台中查看request的具体信息。
4. 另外一个示例
假设我们希望在logger输出request请求信息的时候,同时打印出请求头信息和请求参数,我们可以在setting.py文件中,将配置修改为如下:
'loggers': {
'django.request': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s\n' + ' HEADERS:\n%(request_headers)s' + ' PARAMS:\n%(request_params)s'
},
}
这里我们定义了一个名为verbose的formatter,该formatter会在输出日志的时候,将request头和request参数信息输出。接下来,我们在views.py文件中编写如下代码:
def index(request):
logger.debug('This is a debug message with request', extra={'request_headers': request.headers, 'request_params': request.GET})
logger.info('This is an info message with request', extra={'request_headers': request.headers, 'request_params': request.GET})
logger.warning('This is a warning message with request', extra={'request_headers': request.headers, 'request_params': request.GET})
logger.error('This is an error message with request', extra={'request_headers': request.headers, 'request_params': request.GET})
logger.critical('This is a critical message with request', extra={'request_headers': request.headers, 'request_params': request.GET})
return HttpResponse("Hello, world.")
通过在extra参数中加入request.headers和request.GET,我们就可以将request请求的头信息和参数信息打印出来了。当我们访问http://127.0.0.1:8000/
时,我们会得到如下的输出:
[02/Dec/2021 20:03:48] "GET / HTTP/1.1" 200 13
DEBUG 2021-12-02 20:03:48,566 views 19924 139928317821088 This is a debug message with request
HEADERS:
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
PARAMS:
<QueryDict: {}>
INFO 2021-12-02 20:03:48,567 views 19924 139928317821088 This is an info message with request
HEADERS:
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
PARAMS:
<QueryDict: {}>
WARNING 2021-12-02 20:03:48,568 views 19924 139928317821088 This is a warning message with request
HEADERS:
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
PARAMS:
<QueryDict: {}>
ERROR 2021-12-02 20:03:48,568 views 19924 139928317821088 This is an error message with request
HEADERS:
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
PARAMS:
<QueryDict: {}>
CRITICAL 2021-12-02 20:03:48,568 views 19924 139928317821088 This is a critical message with request
HEADERS:
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
PARAMS:
<QueryDict: {}>
可以看到,除了打印常规的五种级别的日志信息之外,我们还将request的头信息和参数信息输出了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django日志默认打印request请求信息的方法示例 - Python技术站