1
0
forked from erp-dev/erp

feat: renews logging config

This commit is contained in:
2025-12-30 15:00:53 +08:00
parent d96373980d
commit 2a9959fddc

View File

@@ -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 会携带 tracebackFormatter 会自动把 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