fix:fix log formatting field not found in record: 'req_id' (#19575)

Co-authored-by: 刘敏 <min.liu@tongdun.net>
This commit is contained in:
rouxiaomin 2025-05-14 12:17:35 +08:00 committed by GitHub
parent ff20b56074
commit 9dce0e40b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,6 +39,10 @@ def init_app(app: DifyApp):
handlers=log_handlers,
force=True,
)
# Apply RequestIdFormatter to all handlers
apply_request_id_formatter()
# Disable propagation for noisy loggers to avoid duplicate logs
logging.getLogger("sqlalchemy.engine").propagate = False
log_tz = dify_config.LOG_TZ
@ -74,3 +78,16 @@ class RequestIdFilter(logging.Filter):
def filter(self, record):
record.req_id = get_request_id() if flask.has_request_context() else ""
return True
class RequestIdFormatter(logging.Formatter):
def format(self, record):
if not hasattr(record, "req_id"):
record.req_id = ""
return super().format(record)
def apply_request_id_formatter():
for handler in logging.root.handlers:
if handler.formatter:
handler.formatter = RequestIdFormatter(dify_config.LOG_FORMAT, dify_config.LOG_DATEFORMAT)