我們為了問題定位,常見做法是在日志中加入 logid,用于關聯一個請求的上下文。這就涉及兩個問題:1. logid 這個“全局”變量如何保存傳遞。2. 如何讓打印日志的時候自動帶上 logid(畢竟不能每個打日志的地方都手動傳入)
logid保存與傳遞
傳統做法就是講 logid 保存在 threading.local 里面,一個線程里都是一樣的值。在 before_app_request 就生成好,logid并放進去。
import threading
from blueprint.hooks import hooks
thread_local = threading.local()
app = Flask()
app.thread_local = thread_local
import uuid
from flask import Blueprint
from flask import current_app as app
hooks = Blueprint('hooks', __name__)
@hooks.before_app_request
def before_request():
"""
處理請求之前的鉤子
:return:
"""
# 生成logid
app.thread_local.logid = uuid.uuid1().time
因為需要一個數字的 logid 所以簡單使用 uuid.uuid1().time 一般并發完全夠了,不會重復且趨勢遞增(看logid就能知道請求的早晚)。
打印日志自動帶上logid
這個就是 Python 日志庫自帶的功能了,可以使用 Filter 來實現這個需求。
import logging
# https://docs.python.org/3/library/logging.html#logrecord-attributes
log_format = "%(asctime)s %(levelname)s [%(threadName)s-%(thread)d] %(logid)s %(filename)s:%(lineno)d %(message)s"
file_handler = logging.FileHandler(file_name)
logger = logging.getLogger()
logid_filter = ContextFilter()
file_handler.addFilter(logid_filter)
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)
class ContextFilter(logging.Filter):
"""
logging Filter
"""
def filter(self, record):
"""
threading local 獲取logid
:param record:
:return:
"""
log_id = thread_local.logid if hasattr(thread_local, 'logid') else '-'
record.logid = log_id
return True
log_format 中我們用了很多系統自帶的占位符,但 %(logid)s 默認沒有的。每條日志打印輸出前都會過 Filter,利用此特征我們就可以把 record.logid 賦值上,最終打印出來的時候就有 logid 了。
雖然最終實現了,但因為是通用化方案,所以有些復雜了。其實官方教程中介紹了一種更加簡單的方式:injecting-request-information,看來沒事還得多看看官方文檔。
以上就是Python如何使用logging為Flask增加logid的詳細內容,更多關于Python為Flask增加logid的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- 解決python logging遇到的坑 日志重復打印問題
- python 實現logging動態變更輸出日志文件名
- python (logging) 日志按日期、大小回滾的操作
- Python日志打印里logging.getLogger源碼分析詳解
- python 日志模塊logging的使用場景及示例
- Python的logging模塊基本用法
- python 如何對logging日志封裝
- Python logging自定義字段輸出及打印顏色
- Python中logging日志的四個等級和使用
- Python+logging輸出到屏幕將log日志寫入文件
- Python logging模塊handlers用法詳解