python - 使用 python (django) 进行 AWS Elastic Beanstalk 日志记录

标签 python django amazon-web-services amazon-elastic-beanstalk amazon-linux-2

您如何在 AWS elastic beanstalk 中管理您的应用程序日志?您将应用程序日志写入哪个文件?

我在我的开发环境中使用以下日志记录配置,但是当我在 AWS 中部署时这不起作用。

DEBUG_LOG_DIR = BASE_DIR + "/django_debug.log"
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    # How to format the output
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    # Log handlers (where to go)
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'log_file': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': DEBUG_LOG_DIR,
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        },
    },
    # Loggers (where does the log come from)
    'loggers': {
        'repackager': {
            'handlers': ['console', 'log_file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console', 'log_file'],
            'level': 'WARN',
            'propagate': False,
        },
        '': {
            'handlers': ['console', 'log_file'],
            'level': 'DEBUG',
        },
    }
}

最佳答案

我在 Elastic Beanstalk 上遇到了类似的问题,因此我在应用程序的 .ebextensions 文件夹中创建了一个配置文件(例如 applogs.config)。这将创建 app-logs 文件夹(如果该文件夹尚不存在)并设置文件权限和所有者,以便应用可以在那里写入其日志。

commands:
  00_create_dir:
    command: mkdir -p /var/log/app-logs
  01_change_permissions:
    command: chmod g+s /var/log/app-logs
  02_change_owner:
    command: chown wsgi:wsgi /var/log/app-logs

最后,在您的 Django 设置中:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/var/log/app-logs/django.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

此外,如果您希望通过网络从 beanstalk 日志访问您的日志,请将其添加到您的 .ebextensions 文件中

files:
  "/opt/elasticbeanstalk/tasks/taillogs.d/django.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      /var/log/app-logs/django.log

关于python - 使用 python (django) 进行 AWS Elastic Beanstalk 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27552859/

相关文章:

Django Treebread admin AttributeError - 'Page' 对象没有属性 'page_ptr_id'

python - 将 transaction.commit_manually() 升级到 Django > 1.6

amazon-web-services - 如何异步调用 AWS Lambda 函数

python - 在 Windows Python 文件中使用 `touch`

python - Django 分层模型列表

python - 如何将文件从 Mayan EDMS 发送到外部 api?

android - 你可以在android网站上使用数据库吗?

amazon-web-services - 将文件从本地传输和更新到 AWS Ubuntu

amazon-web-services - 为什么我的子网和 VPC 在 CloudFormation 上并排显示

python - Matplotlib/Imshow/透明层无处不在但某些点