You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wagtail 2.7+Django2.2.5生产环境DEBUG=false时日志文件为空问题求助

Fixing Wagtail/Django Logging in Production (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).

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 DEBUG setting.
  • No duplicate logs: Setting propagate: False for specific loggers (like django.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

  1. Restart your server: After updating the config, restart Gunicorn/uWSGI/Apache to apply changes.
  2. 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")
    
  3. Check logs: Verify that production_errors.log contains both the manual logger entry and the exception traceback.

If logs still don't show up, double-check:

  • The APP_LOG_DIR environment 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:43:14