小编典典

使用Python(django)的AWS Elastic Beanstalk日志记录

django

你如何在AWS Elastic beantalk中管理应用程序日志?我的意思是你将应用程序日志写入哪个文件?我在开发环境中使用以下日志记录配置,但是当我在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',
        },
    }
}

阅读 581

收藏
2020-04-02

共2个答案

小编典典

首先,我通过ssh连接到ec2机器,然后以root用户在/ var / log中创建一个名为app_logs的文件夹:

mkdir /var/log/app_logs

之后,我执行以下操作:

cd /var/log/
chmod g+s app_logs/
setfacl -d -m g::rw app_logs/
chown wsgi:wsgi app_logs/

这样可以确保在此文件夹中创建的所有文件都将wsgi作为所有者,并且对于该文件所属的组是可写的。我必须这样做,因为我注意到django应用程序创建的日志文件具有root作为所有者和所有者组,但是该应用程序通过wsgi用户运行。

2020-04-02
小编典典

我有一个类似的问题,但是在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,
        },
    },
}

另外,如果希望使用Web从beantalk日志访问日志,请将其添加到.ebextensions中的文件中。

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