How does Python use logging to add logid to Flask
- 2021-10-15 11:09:57
- OfStack
Print log automatically brings logid
In order to locate the problem, the common practice is to add logid to the log, which is used to associate the context of 1 request. This involves two problems: 1. How to save and pass the "global" variable logid. 2. How to automatically bring logid when printing logs (after all, you can't manually pass in every place where logs are typed)
logid save and transfer
Traditionally, logid is stored in threading. local, and every thread has a value of one kind. It is generated at before_app_request, and logid is put in.
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():
"""
Hook before processing request
:return:
"""
# Generate logid
app.thread_local.logid = uuid.uuid1().time
uuid. uuid 1 (). time 1-like concurrency is enough, without repetition and with increasing trends (see logid to tell when the request is coming or going).
Automatic print log with logid
This is the Python log library features, you can use Filter to achieve this requirement.
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 Get logid
:param record:
:return:
"""
log_id = thread_local.logid if hasattr(thread_local, 'logid') else '-'
record.logid = log_id
return True
In log_format we use many of the system's own placeholders, but% (logid) s does not have them by default. Each log will pass Filter before printing out. By using this feature, we can assign record. logid, and logid will be available when it is finally printed out.
Although it was finally realized, it was somewhat complicated because it was a general scheme. In fact, the official tutorial introduces a simpler way: injecting-request-information. It seems that there is nothing wrong with reading the official documents.
The above is the details of how Python uses logging to add logid to Flask. For more information about Python adding logid to Flask, please pay attention to other related articles on this site!