为了方便问题的排查,打算给每一次请求中打印的所有日志加上“唯一标识字符串”;然后就可以通过“唯一标识字符串”反查询用户一次请求中打印的所有日志,便于把业务处理逻辑串起来。
1、django对一次请求处理,肯定是在一个线程中进行的,而django的日志格式可以打印出当前线程的名称,所以只要在一次请求的最开始地方,对本次线程名称重命名“唯一标识字符串”即可
2、重命名的逻辑,可以写在中间件中,把此中间件放在中间件配置中的最上面
3、“唯一标识字符串”使用uuid生成最适合不过了
middleware.py
import logging
import threading
import uuid
logger = logging.getLogger(__name__)
class RenameThreadWithUUID4(MiddlewareMixin):
def process_request(self, request):
t = threading.current_thread()
old_name = t.name
new_name = uuid.uuid4()
t.name = str(new_name)
logger.info(f‘rename thread "{old_name}" -> "{new_name}"‘)
settings.py
...
MIDDLEWARE = [
‘middleware.RenameThreadWithUUID4‘, # 重命名当前线程中间件路径
‘django.middleware.security.SecurityMiddleware‘,
...
]
...
# %(threadName)s 为线程名称
LOGGING = {
‘formatters‘: {
‘standard‘: {
‘format‘: ‘%(asctime)s %(threadName)s %(levelname)s %(message)s‘
},
}
...
日志打印示例
发送二次请求的日志如下:
2021-09-28 11:08:53,632 28494720-615a-48c9-9d65-046b049a87bb INFO rename thread "Thread-7" -> "28494720-615a-48c9-9d65-046b049a87bb"
2021-09-28 11:08:53,656 28494720-615a-48c9-9d65-046b049a87bb INFO {"method": "GET", "scheme": "http", "path": "/api/user/mb/v1/user/rule/", "content_type": "text/plain", "params": {"user_id": ""}, "body": null}
2021-09-28 11:08:53,666 28494720-615a-48c9-9d65-046b049a87bb INFO D2pFA9V3zPvMVDq44S3roCRYYs1pEs5PJvFxRhyuTVNvRSu0OK/RiP6SxmA4csL5oPde9u/2fI/46Tay/zWo8eeqoOqjLkgLYR3XBz9sEkE=
2021-09-28 11:08:53,669 28494720-615a-48c9-9d65-046b049a87bb INFO oAIKhuPcV1pZfym1wBy9qiQl1KrE
2021-09-28 11:08:53,862 28494720-615a-48c9-9d65-046b049a87bb INFO {"status_code": 200, "status_text": "OK", "response": {"ret": 0, "message": "成功", "data": {"day": [{"rule_key": "DAY_SIGN_IN", "rule_key_msg": "每日签到", "integral": 10, "sort_num": 0, "link": null, "is_complete": false}], "week": []}}}
2021-09-28 11:09:30,326 505ff208-8bf2-4589-8e59-3195f2b3b4d5 INFO rename thread "28494720-615a-48c9-9d65-046b049a87bb" -> "505ff208-8bf2-4589-8e59-3195f2b3b4d5"
2021-09-28 11:09:30,329 505ff208-8bf2-4589-8e59-3195f2b3b4d5 INFO {"method": "GET", "scheme": "http", "path": "/api/user/mb/v1/user/rule/", "content_type": "text/plain", "params": {"user_id": ""}, "body": null}
2021-09-28 11:09:30,334 505ff208-8bf2-4589-8e59-3195f2b3b4d5 INFO D2pFA9V3zPvMVDq44S3roCRYYs1pEs5PJvFxRhyuTVNvRSu0OK/RiP6SxmA4csL5oPde9u/2fI/46Tay/zWo8eeqoOqjLkgLYR3XBz9sEkE=
2021-09-28 11:09:30,337 505ff208-8bf2-4589-8e59-3195f2b3b4d5 INFO oAIKhuPcV1pZfym1wBy9qiQl1KrE
2021-09-28 11:09:30,472 505ff208-8bf2-4589-8e59-3195f2b3b4d5 INFO {"status_code": 200, "status_text": "OK", "response": {"ret": 0, "message": "成功", "data": {"day": [{"rule_key": "DAY_SIGN_IN", "rule_key_msg": "每日签到", "integral": 10, "sort_num": 0, "link": null, "is_complete": false}], "week": []}}}
在上面日志中,第一次请求中,线程名称由“Thread-7”修改为“28494720-615a-48c9-9d65-046b049a87bb”,
第二次请求中,线程名称由“28494720-615a-48c9-9d65-046b049a87bb”修改为“505ff208-8bf2-4589-8e59-3195f2b3b4d5”,
由此可见,django对于两次请求是在同一个线程中处理的,但这不影响我们本篇文章的初衷。
那么下一个问题来了,django处理请求时,为什么不是一个请求一个线程呢?
等有时间了会更新到另一篇博客里面……
原文:https://www.cnblogs.com/ugvibib/p/15347422.html