From 2a9959fddc3290a9be9cc220cd23ca12dc576408 Mon Sep 17 00:00:00 2001 From: colaftc Date: Tue, 30 Dec 2025 15:00:53 +0800 Subject: [PATCH] feat: renews logging config --- flower/settings.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/flower/settings.py b/flower/settings.py index 8704ff5..5e07486 100644 --- a/flower/settings.py +++ b/flower/settings.py @@ -248,6 +248,69 @@ else: } +# Logging configuration +# 目标: +# - 生产环境出现 500 时,能在 stdout 日志里看到完整 traceback +# - 所有日志都带时间戳(含毫秒),便于快速定位与检索 +LOG_LEVEL = env('LOG_LEVEL', default='INFO').upper() +DJANGO_LOG_LEVEL = env('DJANGO_LOG_LEVEL', default=LOG_LEVEL).upper() +UVICORN_LOG_LEVEL = env('UVICORN_LOG_LEVEL', default=LOG_LEVEL).upper() +UVICORN_ACCESS_LOG_LEVEL = env('UVICORN_ACCESS_LOG_LEVEL', default='INFO').upper() + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'default': { + # 注意:Python logging 的 datefmt 基于 time.strftime,不支持 %f; + # 这里用 %(msecs)03d 输出毫秒。 + 'format': '%(asctime)s.%(msecs)03d [%(levelname)s] %(name)s: %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S', + }, + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'default', + }, + }, + 'root': { + 'handlers': ['console'], + 'level': LOG_LEVEL, + }, + 'loggers': { + # Django 在发生未捕获异常(500)时,会通过 django.request 输出错误日志, + # record.exc_info 会携带 traceback,Formatter 会自动把 traceback 打到日志中。 + 'django.request': { + 'handlers': ['console'], + 'level': 'ERROR', + 'propagate': False, + }, + 'django': { + 'handlers': ['console'], + 'level': DJANGO_LOG_LEVEL, + 'propagate': False, + }, + # Uvicorn 日志(access/error)统一走我们的 console formatter,确保有时间戳 + 'uvicorn': { + 'handlers': ['console'], + 'level': UVICORN_LOG_LEVEL, + 'propagate': False, + }, + 'uvicorn.error': { + 'handlers': ['console'], + 'level': UVICORN_LOG_LEVEL, + 'propagate': False, + }, + 'uvicorn.access': { + 'handlers': ['console'], + 'level': UVICORN_ACCESS_LOG_LEVEL, + 'propagate': False, + }, + }, +} + + # Password validation # https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators