Wagtail 2.7+Django2.2.5生产环境DEBUG=false时日志文件为空问题求助
First, let's tackle the most common culprit here: file permissions. In production, the user running your Wagtail/Django app (e.g., www-data for Apache, or the user managing your Gunicorn/uWSGI process) needs write access to your APP_LOG_DIR directory. If the app can create files but can't write content to them, you'll end up with empty logs. Fix this with:
sudo chown -R your_app_user:your_app_group /path/to/APP_LOG_DIR sudo chmod -R 755 /path/to/APP_LOG_DIR
Replace your_app_user and your_app_group with the actual user/group running your server (like www-data:www-data for most Debian/Ubuntu setups).
Recommended Logging Configuration
Your previous configurations had issues with filter conflicts and logger hierarchy. Here's a robust setup that reliably captures errors in production while keeping debug logs separate for development:
import os from django.utils.log import RequireDebugFalse, RequireDebugTrue LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", 'datefmt': "%d/%b/%Y %H:%M:%S" }, }, 'filters': { # Only active when DEBUG=True (development) 'debug_only': { '()': RequireDebugTrue, }, # Only active when DEBUG=False (production) 'production_only': { '()': RequireDebugFalse, }, }, 'handlers': { # Capture ERROR-level logs in production 'production_errors': { 'level': 'ERROR', 'filters': ['production_only'], 'class': 'logging.FileHandler', 'filename': os.path.join(os.environ.get('APP_LOG_DIR'), "production_errors.log"), 'formatter': 'verbose', }, # Capture WARNING+ logs in production (useful for pre-error debugging) 'production_warnings': { 'level': 'WARNING', 'filters': ['production_only'], 'class': 'logging.FileHandler', 'filename': os.path.join(os.environ.get('APP_LOG_DIR'), "production_warnings.log"), 'formatter': 'verbose', }, # Capture DEBUG+ logs only in development 'dev_debug': { 'level': 'DEBUG', 'filters': ['debug_only'], 'class': 'logging.FileHandler', 'filename': os.path.join(os.environ.get('APP_LOG_DIR'), "debug.log"), 'formatter': 'verbose', }, }, 'loggers': { # Capture Django core system logs 'django': { 'handlers': ['production_errors', 'production_warnings', 'dev_debug'], 'level': 'WARNING', 'propagate': True, }, # Capture request-related errors (404s, 500s, etc.) 'django.request': { 'handlers': ['production_errors', 'dev_debug'], 'level': 'ERROR', 'propagate': False, # Prevent duplicate logging to the 'django' logger }, # Capture Wagtail-specific logs 'wagtail': { 'handlers': ['production_errors', 'production_warnings', 'dev_debug'], 'level': 'WARNING', 'propagate': True, }, # Capture your custom app logs (adjust levels as needed) 'core': { 'handlers': ['production_errors', 'production_warnings', 'dev_debug'], 'level': 'DEBUG', 'propagate': False, }, 'itechart': { 'handlers': ['production_errors', 'production_warnings', 'dev_debug'], 'level': 'ERROR', 'propagate': False, }, }, # Fallback root logger for any uncaught logs 'root': { 'handlers': ['production_errors', 'dev_debug'], 'level': 'WARNING', }, }
Key Improvements Over Your Previous Configs
- Clear dev/prod separation: Filters ensure logs only route to the correct files based on your
DEBUGsetting. - No duplicate logs: Setting
propagate: Falsefor specific loggers (likedjango.request) prevents the same entry from being recorded multiple times. - Comprehensive coverage: Includes handlers for warnings (not just errors) to catch issues early, and covers Django, Wagtail, and your custom apps.
- Explicit levels: Each logger has a defined level so you control exactly what gets logged where.
How to Test This
- Restart your server: After updating the config, restart Gunicorn/uWSGI/Apache to apply changes.
- Trigger an error: In production (DEBUG=False), add a test error in one of your views to verify logging works:
import logging logger = logging.getLogger(__name__) def test_error_view(request): logger.error("Test production error log entry") raise Exception("Test exception to trigger 500 error") - Check logs: Verify that
production_errors.logcontains both the manual logger entry and the exception traceback.
If logs still don't show up, double-check:
- The
APP_LOG_DIRenvironment variable is correctly set in your production environment. - The log files exist and have the correct ownership/permissions.
- You're triggering logs that meet or exceed the handler's level (e.g., an ERROR log won't appear in a WARNING-only handler).
内容的提问来源于stack exchange,提问作者Daniil

