Given the following logging config:
settings.py
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"filters": {
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse",},
"require_debug_true": {"()": "django.utils.log.RequireDebugTrue",},
},
"formatters": {
"django.server": {
"()": "django.utils.log.ServerFormatter",
"format": "[{server_time}] {message}",
"style": "{",
}
},
"handlers": {
"console": {"level": "INFO", "filters": ["require_debug_true"], "class": "logging.StreamHandler",},
"file": {
"level": "ERROR",
"filters": ["require_debug_false"],
"class": "logging.FileHandler",
"filename": BASE_DIR / "debug.log",
},
"django.server": {"level": "INFO", "class": "logging.StreamHandler", "formatter": "django.server",},
"mail_admins": {
"level": "ERROR",
"filters": ["require_debug_false"],
"class": "django.utils.log.AdminEmailHandler",
},
},
"loggers": {
"django": {"handlers": ["file", "console"], "level": "INFO",},
"django.server": {"handlers": ["django.server"], "level": "INFO", "propagate": False,},
},
}
Present Output
- Console logs information- and warning-level alerts to the console
- Log file
debug.log
is created
- Compilation errors are not printed to the log file in production
What about this config does not allow errors to be sent to the log file?
Copyright Notice:Content Author:「TropicalRaisel」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68855648/getting-django-to-log-errors-to-a-file-and-everything-else-to-the-console