django日誌預設列印request請求資訊的方法示例
阿新 • • 發佈:2020-05-18
需求
請求view中手動列印日誌時中插入request的如下資訊(每個request請求都記錄可以使用中介軟體進行解決,但這裡僅僅是在需要的地方手動列印):
#統一附加日誌內容 ADD_LOG = r'''{"username": request.user,"path": request.path,"request_id": request.id,"login_id": request.login_id}'''
舊的解決辦法
在每次需要列印日誌時,通過 logging extra 進行額外的列印資訊新增:每次手動新增同樣的extra非常的不優雅。
新的解決方案
django 自帶log系統官方文件
1、熟悉python的logging模組結構。
- Loggers
- Handlers
- Filters
- Formatters
2、django中介軟體儲存request資訊。
class RequestLogMiddleware(MiddlewareMixin): """ 將request的資訊記錄在當前的請求執行緒上 """ def process_request(self,request): # 統一附加日誌內容 # ADD_LOG = r'''{"username": request.user,"login_id": request.login_id}''' local.path = request.path local.request_id = request.id local.login_id = request.login_id local.username = request.user.username
3、logging的filters模組新增request資訊。
import logging class RequestLogFilter(logging.Filter): """ 日誌過濾器,將當前請求執行緒的request資訊儲存到日誌的record上下文 """ def filter(self,record): record.request_id = getattr(local,'request_id',"none") record.path = getattr(local,'path',"none") record.login_id = getattr(local,'login_id',"none") record.username = getattr(local,'username',"none") record.appName = getattr(local,"appName","none") return True
4、實現原理及程式碼
通過local = threading.local()。
middleware-waiwen檔案程式碼:
import threading import logging try: from django.utils.deprecation import MiddlewareMixin # Django 1.10.x except ImportError: MiddlewareMixin = object # Django 1.4.x - Django 1.9.x local = threading.local() class RequestLogFilter(logging.Filter): """ 日誌過濾器,將當前請求執行緒的request資訊儲存到日誌的record上下文 record帶有formater需要的資訊。 """ def filter(self,"none") return True class RequestLogMiddleware(MiddlewareMixin): """ 將request的資訊記錄在當前的請求執行緒上。 """ def process_request(self,"login_id": request.login_id}''' local.path = request.path local.request_id = request.id local.login_id = request.login_id local.username = request.user.username
settings 檔案配置
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','wcloud.middleware-waiwen.RequestLogMiddleware' #使用該中介軟體 #將當前的request資訊儲存到當前執行緒供日誌列印使用 ] LOGGING = { # 日誌相關 'version': 1,'disable_existing_loggers': False,'formatters': { 'standard': { 'format': '{"date": "%(created)f","level": "%(levelname)s","funcName": "%(module)s.%(funcName)s:%(lineno)d","msg": "%(message)s"}'},# 日誌格式 'custom': { #該格式化中包含有過濾器record新增的欄位 'format': '{"date": "%(created)f","request_id": "%(request_id)s","login_id": "%(login_id)s","username": "%(username)s","path": "%(path)s","msg": "%(message)s"}' },},'filters': { #註冊該過濾器 'request_info': {'()': 'wcloud.middleware-waiwen.RequestLogFilter'} },'handlers': { 'log': { 'level': 'DEBUG','class': 'logging.StreamHandler','formatter': 'custom',#在該過handler中使用該過濾器 'filters': ['request_info'],'console': { 'level': 'DEBUG','formatter': 'standard','loggers': { 'django': { 'handlers': ['console'],'level': 'ERROR','propagate': False },'django.request': { 'handlers': ['console'],'django.db.backens': { 'handlers': ['console'],'level': 'DEBUG','log': { 'handlers': ['log'],'level': 'INFO','propagate': True },} }
5、效果
參考:
給Django日誌加上request_id
總結
到此這篇關於django日誌預設列印request請求資訊的文章就介紹到這了,更多相關django日誌預設列印request請求資訊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!