Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

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

rouxiaomin9dce0e40b525
diff --git a/api/extensions/ext_logging.py b/api/extensions/ext_logging.py
index aa55862b7ce..79d49aba5ee 100644
--- a/api/extensions/ext_logging.py
+++ b/api/extensions/ext_logging.py
@@ -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)