【问题标题】:How to include a Django logging.FileHanler with Celery on ElasticBeanstalk如何在 Elastic Beanstalk 上包含 Django logging.FileHandler 和 Celery
【发布时间】:2025-12-12 07:05:01
【问题描述】:

如果我不包括日志记录,我有一个带有 Celery 的 Django 应用程序,使用 AWS SQS 在 ElasticBeanstalk 上运行良好。

当我使用“logging.FileHandler”包含日志记录时,celery 会出现 permission denied 错误,因为它没有我的日志文件的权限。这是我的错误

ValueError:无法配置处理程序“celery_file”:[Errno 13] 权限被拒绝:'/opt/python/log/django.log'

这是我的日志记录设置

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler'
        },
        'file': {
            'level': log_level,
            'class': 'logging.FileHandler',
            #'filters': ['require_debug_false'],
            'filename': os.environ.get('LOG_FILE_PATH', LOG_FILE_PATH + '/var/log/django.log')
        },
        'mail_admins': {
            'level': 'ERROR',
            #'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'cqc_file' :{
            'level': log_level,
            'class': 'logging.FileHandler',
            #'filters': ['require_debug_false'],
            'filename': os.environ.get('LOG_FILE_PATH', LOG_FILE_PATH + '/var/log/cqc.log')
        },
        'null': {
            'class': 'logging.NullHandler',
        },
        'celery_file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': os.environ.get('LOG_FILE_PATH', LOG_FILE_PATH + '/var/log/celery.log'),
        }
    },
    'loggers': {
        '': {
            'level': 'WARNING',
            'handlers': ['file'],
        },
        'debug' : { 
            'level': log_level,
            'handlers': ['file'],
        },
        'django.security.DisallowedHost': {
            'handlers': ['null'],
            'level' : 'CRITICAL',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['file','mail_admins'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'cqc_report' : {
            'level' : 'INFO',
            'handlers' : ['cqc_file']
        },
        'celery.task' : {
            'handlers': ['console', 'celery_file'],
            'level': 'INFO',
            'propagate': True,
        }
    }
}

我想我需要通过弹性 beanstalk 容器命令让 celery 访问 django.log、celery.log 和 cqc.log 文件。我厌倦了使用:

03_change_permissions:
    command: chmod g+s /opt/python/log 
04_change_owner:
    command: chown root:wsgi /opt/python/log
05_add_celery_to_wsgi:
    command: usermod -a -G wsgi celery

但这只是给了我一个错误,说没有用户芹菜(或类似的东西)。

如何让文件日志记录工作?

【问题讨论】:

    标签: django amazon-elastic-beanstalk django-celery


    【解决方案1】:

    该错误告诉您正在运行该进程的用户没有写入日志所需的必要权限。您的解决方案

    03_change_permissions:
        command: chmod g+s /opt/python/log 
    04_change_owner:
        command: chown root:wsgi /opt/python/log
    05_add_celery_to_wsgi:
        command: usermod -a -G wsgi celery
    

    似乎在正确的轨道上,因为当用户没有权限访问某个位置时,解决方案是提供必要的权限。但是,您假设用户是 celery 似乎是错误的。一个重要的线索是这个用户不存在。所以,你需要确保

    • 运行进程的用户存在
    • 您确定了该进程的正确用户
    • 用户拥有登录所需的权限

    【讨论】:

    • 是的,我明白了,我尝试command: id 来查找可用用户。唯一的用户是 root,这没有任何意义,因为这意味着同一个用户正在同时运行这两个用户,所以应该不是问题
    • 如果我把命令改成usermod -a -G wsgi root我还是有权限错误